DropDownList
RangeValidator
分配给它。数据绑定到页面加载时的下拉列表。这些值是从数据库中检索到的,因此每个项的value属性都设置为DB中该项的唯一ID。
RangeValidator类似于:
<asp:rangevalidator id="ddRangeValidator" runat="server" ControlToValidate="ddMenu" ErrorMessage="Please select value in range" MinimumValue="1" MaximumValue="100000" Type="Integer">*</asp:rangevalidator>
我有一个方法可以在jQuery中自动填充这个值。
$("#ddMenu").val("An Option");
但是,当我尝试发布页面时,范围验证失败,这是可行的。然后,即使我手动选择该值,或选择另一个有效值,它仍然不会验证。使其生效的唯一方法是选择无效值,然后重新选择有效值。
更新
以下是数据绑定代码:
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