ASP.NET:当我单击一个LinkBut​​ton时,会引发错误的事件 [英] ASP.NET: Wrong event is fired when I click a LinkButton

查看:75
本文介绍了ASP.NET:当我单击一个LinkBut​​ton时,会引发错误的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a "cart" Repeater, where every item has a couple of controls in it. The ones we're focusing on are two DropDownLists, a LinkButton and a TextBox. The lists are on one validation group and the button & textbox are on another.

两个列表都有一个 OnSelectedIndexChanged 事件处理程序,该处理程序根据两个控件中的选择进行一些计算 DropDownLists 。这两个列表还具有 AutoPostBack = True

Both lists have an OnSelectedIndexChanged event handler which does some calculations based on the selections in both DropDownLists. Both lists also have AutoPostBack="True".

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

The button has an OnClick handler which also does some calculations according to the number in the textbox.

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

I need the calculations to be updated immediately - so I added another data binding for the Repeater - on each event handler

问题是-我可以单击 DropDownLists 之一后,无法从TextBox中获取值。它始终显示为1。在后台, OnSelectedIndexChanged 事件处理程序首先触发 ,按钮的 OnClick 将在它之后触发。 仅在更改列表之一后发生-从那时起,每次我单击该按钮时都会发生。在此之前-所有控件都正常。

The problem is - I can't get the value from the TextBox after I click on one of the DropDownLists. It always comes off as 1. In the background, the OnSelectedIndexChanged event handler fires first, and the buttons' OnClick will fires after it. That only happens after I change one of the lists - and it happens every time I click that button from that moment on. Before that - everything is normal on all controls.

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

What do you think? Below is some code (I hope it's not too much, I included everything that's taking part) - Thank you very much!

这里是转发器模板:

                <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

最后, LinkBut​​ton 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

谢谢:)

推荐答案

我找到了解决方案!

显然是由于viewstate限制而发生的在ASP.NET中,这会导致 OnSelectedIndexChanged 在Page_Load上触发,无论用户是否实际选择了其他值。

Apparently this is happening because of a viewstate limitation in ASP.NET which causes the OnSelectedIndexChanged to fire on Page_Load regardless if the user actually selected a different value.

因此,当事件处理程序触发时,我正在检查是 DropDownLists 调用了我还是其他控件-阻止了事件在不需要时触发-并将所有事件处理保留在页面inta上ct:

So when the event handler fires, I am checking whether it is the DropDownLists which called me or a different control - that prevents the event from firing when it doesn't need to - and keeps all event handling on the page intact:

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

这篇关于ASP.NET:当我单击一个LinkBut​​ton时,会引发错误的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆