Friday, February 25, 2011

asp.net Validation on DropDownList box

Many beginners found problem at the time of to do validation on dropdownlist control.

Let say if in dropdown list items first item is for indicating "select value" follow by others list items. Many developer validate at coding side that if user has selected any items except first value("Select value") but it will do post back & check on server side & resulting take more time.

You can do client validation following way using Rangevalidator

<asp:DropDownList runat="server" ID="ddl1" >
       
<asp:ListItem Value="0" Text="Select value" />
       
<asp:ListItem Value="1" Text="text1" />
       
<asp:ListItem Value="2" Text="text2" />
   
</asp:DropDownList>
   
<asp:RangeValidator ErrorMessage="Please select value" ControlToValidate="ddl1" runat="server"
       
MinimumValue="1" MaximumValue="100000000" Type=Integer />
   
<asp:Button Text="text" runat="server"  /> 

Here you have to use Range validator with dropdown list & set mininmum value greater then 0 & maximum value to set any max value , aslo provide type value of min & max and that is integer

1 comment:

pratik021 said...

Good work Keep it up
cheer2up