Friday, October 9, 2009

How to set required field validator for dropdownlist

My colleague Sweta asked me to how to set required field validator for doopdownlist.
so to set required field validator for doopdownlist we need to follow these steps.

1. let your dropdown list in such format that first item of it is "--Select--" as in example give below.

2. if your valumember is integer type then set value '0' of valuemamber
for "--Select--"

3. take required field validator and select your dropdownlist as control to validate and in field "Initial Value" enter '0' (without single quotes)

Note: if you are using GUID (uniqueidentifier) or any other type of value as valuemember of dropdownlist then place value of "-- select--" in "Initial Value" field of required field validator.

< asp:DropDownList ID="DropDownList1" runat="server" Height="37px" Width="191px" >
< asp:ListItem Value="0" >Select < /asp:ListItem >
< asp:ListItem >--Select-- < /asp:ListItem >
< asp:ListItem >bb < /asp:ListItem >
< asp:ListItem >cc < /asp:ListItem >
< asp:ListItem >dd < /asp:ListItem >
< /asp:DropDownList >
< asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="DropDownList1" ErrorMessage="RequiredFieldValidator"
InitialValue="0" > < /asp:RequiredFieldValidator >

Share This!

No comments:

Powered By Blogger · Designed By Seo Blogger Templates