Question

I have a DropDownList that contains a few items. I would like to validate this control using a RequiredFieldValidator. My ASP.NET code looks like this:

<asp:DropDownList runat="server" ID ="ddlType"></asp:DropDownList>
<asp:DropDownList runat="server" ID ="ddlItems"></asp:DropDownList>
<asp:RequiredFieldValidator ID="valType" runat="server" ControlToValidate="ddlItems" Display="Dynamic" ErrorMessage="* Please select an option"></asp:RequiredFieldValidator>
<asp:Button ID="btnSubmit" runat="server" text="Save" />

And my Page_Load function in CodeBehind is like this:

    With ddlType
        .Items.Add(New ListItem("-- Select --", "-- Select --"))
        .Items.Add(New ListItem("Grocery", "10"))
        .Items.Add(New ListItem("Stationery", "30"))
    End With  

I have another function that loads the second dropdownlist based on the choice made in the first dropdownlist:

Protected Sub ddlType_Load(sender As Object, e As System.EventArgs) Handles ddlType.Load
    If ddlType.SelectedValue = "10" Then
        With ddlItems
            .Items.Clear()
            .Items.Add(New ListItem("-- Select --", "-- Select --"))
            .Items.Add(New ListItem("Milk", "11"))
            .Items.Add(New ListItem("Eggs", "12"))
            .Items.Add(New ListItem("Cheese", "13"))
            .Items.Add(New ListItem("Bread", "14"))
        End With
    Else
        With ddlItems
            .Items.Clear()
            .Items.Add(New ListItem("-- Select --", "-- Select --"))
            .Items.Add(New ListItem("Pen", "31"))
            .Items.Add(New ListItem("Pencil", "32"))
            .Items.Add(New ListItem("Blank Paper", "33"))
            .Items.Add(New ListItem("Ruled Paper", "34"))
            .Items.Add(New ListItem("Eraser", "35"))
            .Items.Add(New ListItem("Ruler", "36"))
            .Items.Add(New ListItem("Protractor", "37"))
            .Items.Add(New ListItem("Permanent Markers", "38"))
        End With
    End If
End Sub

Following is my code for Submit button:

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        If Page.IsValid Then
            ' Perform SQL Data inserts here.
        End If
End Sub

Whenever I try to submit the data, the RequiredFieldValidator shows the '* Please select an option' error message even though I select an option other than '-- Select --' in the 'ddlItems' dropdownlist. I've tried to submit without selecting any options, as well as after selecting each option. Somehow the submit button's If Page.IsValid condition never has the True value, and the SQL Inserts are never reached.

Could someone please help me out with what I might be missing here? Thank you very much for reading.

Was it helpful?

Solution

Try the following:

  1. Remove the code in the ddlType_Load event. Instead, place that code in the ddlType_SelectedIndexChanged event.

  2. In the Page_load event, modify the code so that the ddlType is populated only when Page.IsPostBack = False.

  3. In the ASPX, add the "initial value" attribute to the RequiredFieldValidator. For example, <asp:RequiredFieldValidator InitialValue="-- Select --" .../>

  4. In the ASPX, add the auto post-back attribute to the ddlType control. For example <asp:DropDownList AutoPostBack = "true" ...></asp:DropDownList>

The reason the page was not valid was because the ddlItems drop-down (which is a required field) was being re-populated (or reset) on page load.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top