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
769 views
in .Net Framework by 66 69 85

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 66 69 85

How to add initial option and ignore this option in the Required Validator in ASP.NET?

To add initial option and ignore this option in the Required Validator in ASP.NET, you have to do the following:

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>

Now, the --- select one --- option is the initial value in dropdown list.

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