Need DropDownValidator Control

  • Thread starter Thread starter Jonathan Wood
  • Start date Start date
J

Jonathan Wood

I need a validator control to fail if the first item in the dropdown list is
selected.

I'm new to .NET and am finding the problems associated with writing my own
(custom controls, client-side event handlers, etc.) more than I may want to
take on.

Thanks for any suggestions of where I might look.

Jonathan
 
Hi Jonathan,

Is it possible that the RangeValidator could do what you need? If you have
non-zero values in the dropdown, you can set the --Select-- value to 0 and
trap for that. If not, a CustomValidator could check that the ddl is not 0.

<form id="Form1" method="post" runat="server">
<P>
<asp:DropDownList id="DropDownList1" runat="server">
<asp:ListItem Value="0">--Select--</asp:ListItem>
<asp:ListItem Value="1">Red</asp:ListItem>
<asp:ListItem Value="2">Green</asp:ListItem>
<asp:ListItem Value="3">Blue</asp:ListItem>
</asp:DropDownList>
<asp:RangeValidator id="RangeValidator1" runat="server"
ErrorMessage="You Must Select" ControlToValidate="DropDownList1"
MaximumValue="3" MinimumValue="1"></asp:RangeValidator></P>
<P>
<asp:Button id="Button1" runat="server" Text="Submit"></asp:Button></P>
</form>

Ken
Microsoft MVP [ASP.NET]
 
Ken,
Is it possible that the RangeValidator could do what you need? If you have
non-zero values in the dropdown, you can set the --Select-- value to 0 and
trap for that. If not, a CustomValidator could check that the ddl is not 0.

<form id="Form1" method="post" runat="server">
<P>
<asp:DropDownList id="DropDownList1" runat="server">
<asp:ListItem Value="0">--Select--</asp:ListItem>
<asp:ListItem Value="1">Red</asp:ListItem>
<asp:ListItem Value="2">Green</asp:ListItem>
<asp:ListItem Value="3">Blue</asp:ListItem>
</asp:DropDownList>
<asp:RangeValidator id="RangeValidator1" runat="server"
ErrorMessage="You Must Select" ControlToValidate="DropDownList1"
MaximumValue="3" MinimumValue="1"></asp:RangeValidator></P>
<P>
<asp:Button id="Button1" runat="server"
Text="Submit"> said:

Don't the validators check just the control's text?

I'm not certain how to interpret ASP code as HTML like you posted
above--I've just been adding controls to the designer and editing the VB or
CS source code. Are you doing something special to make it test the index
rather than the text?

Thanks!

Jonathan
 
Hi Jonathan,

The Rangevalidator tests the selected value in the dropdownlist, not the
index or the text.

In this case the value is a number.

Ken
 
Ken,
The Rangevalidator tests the selected value in the dropdownlist, not the
index or the text.

In this case the value is a number.

Okay, I got you now. I believe the values are optional but I'll look into
this project and see if they can be used.

Thanks.
 
Sorry for jumping in so late. The RequiredFieldValidator would have solved
this much more easily.
1. You set the Value property of the first ListItem to some text that is
unique.
2. Add the RequiredFieldValidator.
3. Set its InitialValue property to the same value as in #1.

FYI: If you want to validate by the index of a dropdownlist, I have a
solution within my product "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx. It has 22 validators that include
handling listboxes, dropdownlists, radiobuttons, checkboxes,
radiobuttonlists, and checkboxlists.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 
Back
Top