Click here to Skip to main content
15,909,440 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to apply validation on dropdown list which is bounded with database
Posted

XML
<asp:DropDownList ID="ddlSuggArea" runat="server" DataSourceID="sdsSuggArea" DataTextField="Area_Desc"

DataValueField="Area_Id" AppendDataBoundItems="True" CausesValidation="True">

<asp:ListItem Selected="True" Value="0">Choose Area...</asp:ListItem>

</asp:DropDownList>

<asp:RequiredFieldValidator ID="rfvSuggArea" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="ddlSuggArea" SetFocusOnError="True" InitialValue="0">Please choose a topic area</asp:RequiredFieldValidator>
 
Share this answer
 
add required field validator as:

XML
<asp:RequiredFieldValidator
    ID="your_id"
    runat="server"
    ErrorMessage="Please Select Value"
    ControlToValidate="your_dropdownlist"
    InitialValue="-Select-"
    Display="dynamic"
    Enabled="false">
</asp:RequiredFieldValidator>


Once you bind the datasource to the Dropdownlist, insert first option as "Please Select".
Set the Intial value in Required field validator.

New value can be added in dropdown as follows:
your_dropdownlist.Items.Insert(0, new ListItem("-Select-","-Select-");
 
Share this answer
 
Comments
Zukiari 27-Feb-12 9:10am    
I am having some dropdown list in my project. I've applied validation control to dropdown list as discussed above. If dropdownlist is selected when page is loaded, then also validation message is appearing if that value didn't change which is unwanted for me. How can I solve this problem?

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