Home All Groups Group Topic Archive Search About

validator for drop down list box

Author
11 Jul 2005 5:56 PM
Paul
Just wondering if anyone knows how to set up a validator for a dropdown list
box, which one to use and what is the validator formula if any.  I do not
what to allow a post back if the selected index = 0, ok to if anything else.
Thanks.
--
Paul G
Software engineer.

Author
12 Jul 2005 2:52 PM
Teemu Keiski
Hi,

You can do that with RequiredFieldValidator. Just give it a value as string
into InitialValue attribute/property which indicates the cvalue in
DropDownList when no selection is made.

For example

<asp:DropDownList ID="ddl" runat="server">
    <asp:ListItem Value="0">Zero</asp:ListItem>
    <asp:ListItem Value="1">One</asp:ListItem>
    <%-- More items here ... --%>
</asp:DropDownList>

<asp:requiredFieldValidator ID="rfv1" ControlToValidate="ddl" runat="server"
InitialValue="0" ErrorMessage="Please make a selection" />

Now it understands that item with value "0" is the default one and indicates
when nothing is selected.

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
Author
12 Jul 2005 3:08 PM
Paul
ok thanks for the information.  I was not sure how to give it a value that it
would consider not valid and did not see in the properties box of the
validator a way to do this.
--
Paul G
Software engineer.


Show quoteHide quote
"Teemu Keiski" wrote:

> Hi,
>
> You can do that with RequiredFieldValidator. Just give it a value as string
> into InitialValue attribute/property which indicates the cvalue in
> DropDownList when no selection is made.
>
> For example
>
> <asp:DropDownList ID="ddl" runat="server">
>     <asp:ListItem Value="0">Zero</asp:ListItem>
>     <asp:ListItem Value="1">One</asp:ListItem>
>     <%-- More items here ... --%>
> </asp:DropDownList>
>
> <asp:requiredFieldValidator ID="rfv1" ControlToValidate="ddl" runat="server"
> InitialValue="0" ErrorMessage="Please make a selection" />
>
> Now it understands that item with value "0" is the default one and indicates
> when nothing is selected.
>
> --
> Teemu Keiski
> ASP.NET MVP, AspInsider
> Finland, EU
> http://blogs.aspadvice.com/joteke
>
>
>