ASP.NET:单击LinkBut​​ton时会触发错误的事件

时间:2012-05-09 15:57:55

标签: asp.net data-binding event-handling drop-down-menu linkbutton

我有一个“购物车”Repeater,其中每个项目都有几个控件。我们关注的是两个DropDownLists,一个LinkButton和一个TextBox。列表位于一个验证组和按钮&文本框在另一个文本框中。

两个列表都有一个OnSelectedIndexChanged事件处理程序,它根据DropDownLists中的选择进行一些计算。两个列表也有AutoPostBack="True"

该按钮有一个OnClick处理程序,它还根据文本框中的数字进行一些计算。

我需要立即更新计算 - 所以我在每个事件处理程序上为Repeater添加了另一个数据绑定

问题是 - 点击其中一个DropDownLists后,我无法从TextBox中获取值。它始终为1.在后台,OnSelectedIndexChanged事件处理程序触发第一个,按钮“OnClick将在其后触发。更改其中一个列表后,仅发生 - 每当我从那一刻点击该按钮时就会发生这种情况。在此之前 - 所有控件上的一切都正常。

你怎么看?下面是一些代码(我希望它不是太多,我包括了所有参与的内容) - 非常感谢你!

这是转发器模板:

                <ItemTemplate>
                    <tr>
                        <td class="size"><div><asp:DropDownList runat="server" ID="_selectSize" AutoPostBack="true" OnSelectedIndexChanged="selectChange" EnableViewState="true" TabIndex="<%#Container.ItemIndex%>" ValidationGroup="doNotValidate"></asp:DropDownList></div></td>
                        <td class="material"><div><asp:DropDownList runat="server" ID="_selectMaterial" AutoPostBack="true" OnSelectedIndexChanged="selectChange" EnableViewState="true" TabIndex="<%#Container.ItemIndex%>" ValidationGroup="doNotValidate"></asp:DropDownList></div></td>
                        <td class="quantity">
                            <div>
                                <div class="quantity_container"><asp:TextBox runat="server" ID="_txtAmount" name="quantity_<%#Container.ItemIndex%>" ValidationGroup="vCart"></asp:TextBox></div>
                                <div><asp:LinkButton runat="server" CssClass="refresh" id="_refreshCart" ValidationGroup="vCart"></asp:LinkButton></div>
                            </div>
                        </td>
                    </tr>
                </ItemTemplate>

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 OrderItem As ProxyMarket.Item = CType(e.Item.DataItem, ProxyMarket.Item)
        Dim btnRemoveProduct As LinkButton = CType(e.Item.FindControl("_removeProduct"), LinkButton)
        Dim btnRefreshCart As LinkButton = CType(e.Item.FindControl("_refreshCart"), LinkButton)
        Dim txtAmount As TextBox = CType(e.Item.FindControl("_txtAmount"), TextBox)
        Dim sizeSelect As DropDownList = CType(e.Item.FindControl("_selectSize"), DropDownList)
        Dim materialSelect As DropDownList = CType(e.Item.FindControl("_selectMaterial"), DropDownList)

        btnRemoveProduct.CommandName = OrderItem.ID
        btnRefreshCart.CommandName = OrderItem.ID
        txtAmount.Text = OrderItem.Units

        AddHandler btnRemoveProduct.Click, AddressOf removeProduct
        AddHandler btnRefreshCart.Click, AddressOf refreshCart

        sizeSelect.DataSource = sizeList
        sizeSelect.DataBind()
        sizeSelect.SelectedIndex = s
        materialSelect.DataSource = materialList
        materialSelect.DataBind()
        materialSelect.SelectedIndex = m
    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
        s = sender.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
        s = ddl.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.Alucobond
    End Select

    Cart.SaveOrder()

    s = sender.SelectedIndex
    m = selectedIndex

    rptCart.DataSource = Cart.Order.Items
    rptCart.DataBind()
End Sub

最后,LinkButton OnClick处理程序:

Protected Sub refreshCart(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim btnRefreshCart As LinkButton = CType(sender, LinkButton)
    Dim amountStr As String
    Dim amount As Integer

    amountStr = CType(btnRefreshCart.Parent.FindControl("_txtAmount"), TextBox).Text
    If IsNumeric(amountStr) Then
        amount = CDbl(amountStr)
    Else
        amount = -1
    End If
    amount = IIf(amount > 0, amount, -30)

    For Each Item As ProxyMarket.Item In Cart.Order.Items
        If Item.ID = btnRefreshCart.CommandName Then
            Item.Units = amount
            Cart.Edit_Product(btnRefreshCart.CommandName, amount)
            Exit For
        End If
    Next

    rptCart.DataSource = Cart.Order.Items
    rptCart.DataBind()
End Sub

谢谢:)

2 个答案:

答案 0 :(得分:1)

我找到了解决方案!

显然这是因为ASP.NET中的视图状态限制导致OnSelectedIndexChanged在Page_Load上触发,无论用户是否实际选择了不同的值。

因此,当事件处理程序触发时,我正在检查它是否是调用我的DropDownLists或不同的控件 - 阻止事件在不需要时触发 - 并保持所有事件处理页面完好无损:

Protected Sub selectChange(ByVal sender As DropDownList, ByVal e As System.EventArgs)
    Dim senderClientID = Page.Request.Params.Get("__EVENTTARGET")
    ' Run ONLY if the relevant select control was clicked,
    ' otherwise this is only fired because of ASP.NET limitations,
    ' and will screw up the original event:
    If senderClientID.IndexOf("_selectSize") <> -1 Or senderClientID.IndexOf("_selectMaterial") <> -1 Then
        'do stuff

答案 1 :(得分:0)

我知道这已经得到了解答,但我遇到了一个非常相似的问题,有一个完全不同的解决方案,因为这是谷歌中出现的第一篇帖子,我想我会在这里添加它。

我有一个链接按钮,当点击它时,但仅在IE9中,它似乎激活页面上的另一个按钮并调用它的事件而不是它自己的事件。

经过多次搞乱和谷歌搜索后,我意识到如果我点击页面特定部分的任何地方,那不仅仅是我的按钮激活了另一个按钮。

我在完全剥离页面后最终发现了问题。就是这样:

   <label style="width: 100%" />

空标签。删除后一切都很好。