RangeValidator not working when selecting DropDown

2019-08-01 07:36发布

问题:

I inherited a site which I am in the middle of updating which has a DropDownList and a RangeValidator assigned to it. The data is bound to the dropdown on Page_Load. The values are retrieved from the database so the value property of each item is set to the unique ID of the item in the DB.

The RangeValidator looks something like:

<asp:rangevalidator id="ddRangeValidator" runat="server" ControlToValidate="ddMenu" ErrorMessage="Please select value in range" MinimumValue="1" MaximumValue="100000" Type="Integer">*</asp:rangevalidator>

I have a method which automatically populates this value in jQuery e.g.

$("#ddMenu").val("An Option");

This works, however, when I try to post the page the range validation fails. Then even if I manually select that value, or select another valid value it still won't validate. The only way to make it validate is to select non-valid value and then re-selecting a valid one.

Any ideas?

Update

Here is the data binding code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load

    If Not Page.IsPostBack Then
        Dim ds As New DataSet()
        Dim myDbObject As New myDbObject()
        ds = myDbObject.ToDataSet() // retrieves all objects from the database

        // store the results in a temporary view to filter
        Dim dv As DataView
        dv = New DataView(ds.Tables(0), "IsLive = True", "ID", DataViewRowState.CurrentRows)

        Dim i As Integer = 0
        ddMenu.Items.Add("Select a value")
        ddMenu.Items.Item(0).Value = 0

        // add all objects from filtered list into drop down menu
        For i = 0 To dv.Count - 1
            With ddMenu.Items
                // add a new item
               .Add(dv.Item(i).Item("Name")) 
                // set the Value property as unique ID of object
               .Item(i + 1).Value = dv.Item(i).Item("ID")
            End With
        Next
    End If

End If

回答1:

Turns out it was an issue with my page loading twice...so now the issue is going to be tracking down why it is loading twice!



回答2:

I guess the Range validator verifies the actual "value" of the dropdown list. May it be possible that by using jQuery to populate your dropdown the actual entry doesn't get a proper option "value"? Could you maybe post the rendered HTML code of your dropdown list?