validator for drop down list box

P

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.
 
T

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.
 
P

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,201
Latest member
KourtneyBe

Latest Threads

Top