I just came across this article on how to use a required field validator on a dropdownlist control - I think the beauty of it is it's simplicity:
By setting the Required Field's InitialValue property to be the same as the Dropdownlist's default value, we can force the validator to "kick off" if the value isn't changed.
Thanks to Bilal Haidar for the solution:
<asp:DropDownList id="DropDownList1" runat="server">
<asp:ListItem Value="-1">--> Choose <--</asp:ListItem>
<asp:ListItem Value="1">Lebanon</asp:ListItem>
<asp:ListItem Value="2">Kuwait</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator id="RequiredFieldValidator1"
runat="server" ErrorMessage="You must select a valid option"
ControlToValidate="DropDownList1" InitialValue="-1" Display="Dynamic" />
<P>
<asp:Button id="Button1" runat="server" Text="Post Back" />
</P>
Validate DropDownList - Bilal Haidar [MVP]
Tags: Dropdownlist, RegularFieldValidator, validation, ASP.NET 2.0, MVP
Currently rated 5.0 by 1 people
- Currently 5/5 Stars.
- 1
- 2
- 3
- 4
- 5