user1216456
user1216456

Reputation:

RequiredValidator for Drop Down not firing (asp:dropdown populated from database)

<asp:DropDownList id="Code1" runat="server" 
    OnLoad="GetCode1" ValidationGroup="ValidateGroup"/>    
 <asp:RequiredFieldValidator ID="Code1_RequiredValidator" runat="server" 
       ErrorMessage="Please select a value!"   ControlToValidate="Code1" 
       ValidationGroup="ValidateGroup" InitialValue="Select Code1!">
 </asp:RequiredFieldValidator>

the drop down is populated in the code behind as shown below:

protected void GetCode1(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ISROManagement sroMgmt = ObjectFactory.CreateSROManagement();
                List<string> code1List = QuerydBForCodes();
                Code1.DataSource = codeList;
                Code1.DataBind();
                Code1.Items.Insert(0, "Make a selection."); //Added to be shown as the default value:
            }
        }

When I submit the modal pop up extender -with the default value at index 0 i.e. Make a Selection - I don't see the ErrorMessage="Please make a selection!" printed next to the drop down! So the validation does not happen.

Also in the button click which submits the modal pop up, i have a

Page.Validate("ValidateGroup");
        if (!Page.IsValid) return;

However, I dont see the validation message printed next to the drop down!

Upvotes: 0

Views: 967

Answers (2)

user1465587
user1465587

Reputation:

In GetCode1 function replace following line:

Code1.Items.Insert(0, "Make a selection.");

To

Code1.Items.Insert(0, "");

Upvotes: 1

fenix2222
fenix2222

Reputation: 4730

Your drop down list must have the same ValidationGroup. Add ValidationGroup="ValidateGroup" to your drop down list

Change InitialValue from "Select Code1!" to "Make a selection."

Upvotes: 0

Related Questions