DropDownLists
上每个项目有两个Repeater
。
我将两个列表绑定到转发器DataBound
上的两个不同列表。
两个列表都有一个OnSelectedIndexChanged
事件处理程序,它根据DropDownLists
中的选择进行一些计算。两个列表也有AutoPostBack="True"
。
我需要立即更新计算。所以我在转发器上添加了另一个数据绑定 - 在列表的事件处理程序上。
然而,这是问题 - 转发器将选择“重置”为-1,最终显示DropDownLists
中的第一项。
如何在数据绑定后确保选择保留?
这是转发器结构:
<asp:Repeater runat="server" ID="rptCart">
<ItemTemplate>
<tr>
<td class="size"><div><asp:DropDownList runat="server" ID="_selectSize" AutoPostBack="true" OnSelectedIndexChanged="selectChange" EnableViewState="true" TabIndex="<%#Container.ItemIndex%>"></asp:DropDownList></div></td>
<td class="material"><div><asp:DropDownList runat="server" ID="_selectMaterial" AutoPostBack="true" OnSelectedIndexChanged="selectChange" EnableViewState="true" TabIndex="<%#Container.ItemIndex%>"></asp:DropDownList></div></td>
</tr>
</ItemTemplate>
</asp:Repeater>
转发器DataBound
:
Protected Sub rptCart_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptCart.ItemDataBound
If e.Item.ItemType = ListItemType.AlternatingItem OrElse e.Item.ItemType = ListItemType.Item Then
Dim sizeSelect As DropDownList = CType(e.Item.FindControl("_selectSize"), DropDownList)
Dim materialSelect As DropDownList = CType(e.Item.FindControl("_selectMaterial"), DropDownList)
sizeSelect.DataSource = sizeList
sizeSelect.DataBind()
materialSelect.DataSource = materialList
materialSelect.DataBind()
End If
End Sub
最后是DropDownLists
事件处理程序:
Protected Sub selectChange(ByVal sender As DropDownList, ByVal e As System.EventArgs)
Dim listing As New PriceListing
Dim ddl As DropDownList
Dim selectedIndex As Integer
If sender.ID = "_selectSize" Then
For Each rptrItem As RepeaterItem In rptCart.Items
ddl = CType(rptrItem.FindControl("_selectMaterial"), DropDownList)
If ddl.TabIndex = sender.TabIndex Then Exit For
Next
For Each listing In artDecoPricing
If listing.Size = sender.SelectedValue Then Exit For
Next
selectedIndex = ddl.SelectedIndex
ElseIf sender.ID = "_selectMaterial" Then
For Each rptrItem As RepeaterItem In rptCart.Items
ddl = CType(rptrItem.FindControl("_selectSize"), DropDownList)
If ddl.TabIndex = sender.TabIndex Then Exit For
Next
For Each listing In artDecoPricing
If listing.Size = ddl.SelectedValue Then Exit For
Next
selectedIndex = sender.SelectedIndex
End If
Select Case selectedIndex
Case 0
Cart.Order.Items(sender.TabIndex).PriceUnit = listing.Canvas
Case 1
Cart.Order.Items(sender.TabIndex).PriceUnit = listing.Acrylic
Case 2
Cart.Order.Items(sender.TabIndex).PriceUnit = listing.Framed
Case 3
Cart.Order.Items(sender.TabIndex).PriceUnit = listing.Framed
End Select
Cart.SaveOrder()
rptCart.DataSource = Cart.Order.Items
rptCart.DataBind()
End Sub
非常感谢你!
答案 0 :(得分:1)
您可以存储旧选择:
Dim sizeSelect As DropDownList = CType(e.Item.FindControl("_selectSize"), DropDownList)
Dim materialSelect As DropDownList = CType(e.Item.FindControl("_selectMaterial"), DropDownList)
Dim sizeSelectedIndex = sizeSelect.SelectedIndex
Dim materialSelectedIndex = materialSelect.SelectedIndex
' do the databinding ... '
sizeSelect.SelectedIndex = If(sizeSelectedIndex < sizeSelect.Items.Count -1, sizeSelectedIndex, -1)
materialSelect.SelectedIndex = If(materialSelectedIndex < materialSelect.Items.Count -1, materialSelectedIndex, -1)