Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
25 views
in .Net Framework by 21 52 67

I have my Dropdown list and already bind the data but I need to add an initial option like "--- select one ---" Also the Dropdown list is required using "asp:RequiredFieldValidator", I need to ignore the initial option in the "asp:RequiredFieldValidator".

My question is How to add initial option and ignore this option in Required Validator?


1 Answer

0 like 0 dislike
by 21 52 67
 
Best answer

Add initial option and ignore this option in Required Validator

You need to change in two things:

1- On pageLoad function add the below:

ddlCity.Items.Insert(0, new ListItem("--- select one ---",  "-1"));

2- In front code in specific in RequiredFieldValidator
Add InitialValue="-1"

<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" CssClass="requiredMsg"
        ValidationGroup="AddRequest" ControlToValidate="ddlCity" InitialValue="-1" 
        Text="Required Field" Display="Dynamic"></asp:RequiredFieldValidator>

By that the --- select one --- option is initial value.

If you don’t ask, the answer is always NO!
...