【问题标题】:ASP.NET - Keep DropDownList selection on repeater DataBoundASP.NET - 在转发器 DataBound 上保留 DropDownList 选择
【发布时间】:2012-05-09 11:15:10
【问题描述】:

Repeater 上的每个项目都有两个 DropDownLists

我将两个列表绑定到转发器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

非常感谢您!

【问题讨论】:

    标签: asp.net data-binding drop-down-menu postback repeater


    【解决方案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)
    

    【讨论】:

    • 成功了!非常感谢你。现在我遇到了另一个问题。我在每个项目上都有一个额外的按钮控件来更新项目数量。它工作正常 - 直到我更改其中一个下拉菜单上的选择。甚至当我按下链接按钮时会发生什么,它首先触发 OnSelectedIndexChanged 事件处理程序,然后才触发 button.click 处理程序! (并且它正在制动该功能)。你怎么看?
    • @user1049693:问它另一个问题,因为很难理解评论中发生了什么。
    • 我的解决方案有点问题 :) 您的解决方案导致转发器中的所有项目的 DropDownLists 值与已更改的项目相同。我需要能够再做两件事:第一件事是将更改限制为仅选择的下拉列表(而不是其余的)并保存所有项目的所有下拉列表的值,以防止它们被在 DataBound 中重置。谢谢!
    • @pilau:为什么只对之前选择的?那些未被选中的将在之后被取消选择,因为它们的 SelectedIndex 是-1。我完全不明白你的第二个要求。 save 是什么意思,为什么需要它?
    • 感谢您保持联系。我的意思是:首先,所有 DDL 在转发器 DataBound 期间都会重置。如果我在转发器中有超过 1 个项目,并且我想在每个 PostBack 之后保留这些值(请记住,我需要根据 DDL 选择的值进行一些计算) - 您的解决方案不适用。难道没有更“动态”的方式来实现这一点吗?而不是创建一个全局 DDL 值集合;例如...谢谢。
    猜你喜欢
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    • 1970-01-01
    • 2011-07-31
    相关资源
    最近更新 更多