Web developers wiki ASP.NET Sitecore Sharepoint Kentico by Evident Interactive

RequiredFieldValidator for DropDownList

Modified: 2008/07/31 12:09 by BHammendorp - Categorized as: ASP.NET
In some cases, it can be handy to use a RequiredFieldValidator for an ASP.NET DropDownList. If you want to validate the selected item so that the first item must not be selected, a RequiredFieldValidator may be usefull.

The first item can be something like 'Choose an item from the list':

<asp:DropDownList runat="server" ID="myList">
<asp:ListItem Text="Choose an item from the list" Value="Choose an item from the list" />
<asp:ListItem Text="item 1" Value="item 1" />
<asp:ListItem Text="item 2" Value="item 2" />
<asp:DropDownList>

<asp:RequiredFieldValidator runat="server" ID="myValidator" ControlToValidate="myList" 
InitialValue="Choose an item from the list" ErrorMessage="You mus select a valid item from the list" />

 © Evident Interactive BV