Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi sir;
I have a dropdownlist in my webForm and bind the items from database. But I unable to make field validator for drpdownlist. I know for dropdownlist to Select initial value but it doesn't work.

please help me;
thanks for advance;
here is my code:

XML
<asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server"
                                       ControlToValidate="drpclassno" ErrorMessage="Please select class"; ForeColor="Red"
                                       InitialValue="--Select Class--"></asp:RequiredFieldValidator>
Posted

XML
<asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ControlToValidate="drpclassno" ErrorMessage="Please select class" ForeColor="Red" InitialValue="0"></asp:RequiredFieldValidator>


and after binding dropdown write this code

DropdownListID.Items.Insert(0, new ListItem("Select Parent Category", "0"));


It will work
 
Share this answer
 
v3
Comments
Wajid Khaksar 30-May-13 5:19am    
Thanks a lot Sir.
Remove semicolon from validator you have used in

ErrorMessage="Please select class";
 
Share this answer
 
Required Field Validator is used to bind user to not leave the field empty... But if you do not give empty list item in drop down list than there is no need for the validator,

If you are binding the Drop Down List from Database than don't allow any empty row in DB, so that no validation is required, because all the time drop down list would be having populated with at least one item,

Further if you still want to correct your given piece of Validator code in Question, than follow the solution provided by Masoom Mir[Solution 2] and along with that put the

InitialValue=" "

i.e. to empty white space,, that will work..

Regards,

:)
 
Share this answer
 
XML
<td align="left" valign="top">
                           <asp:DropDownList ID="ddlPresentStatus" runat="server" CssClass="dropdowncontrol">
                           </asp:DropDownList>
                           <asp:RequiredFieldValidator ID="rflanguage" runat="server" ControlToValidate="ddlPresentStatus"
                               ValidationGroup="a" CssClass="rfmsg" InitialValue="" ErrorMessage="*Please select Present Status" />
                       </td>

aspx.cs:
ddlPresentStatus.Items.Insert(0, new ListItem("select", " "));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900