Just Dean
Mr Collins is my Dad

Validate DropDownList - Bilal Haidar [MVP]

April 26, 2008 12:15 by Dean

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]

Blogged with the Flock Browser

Tags: , , , ,


Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Technical
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag




Live preview

January 6. 2009 23:26

Gravatar