0

錯誤事件被激發我有一個「購物車」 Repeater,其中每個項目有一對夫婦在它的控制。我們正在關注的是兩個DropDownLists,一個LinkButton和一個TextBox。這些列表位於一個驗證組中,並且按鈕&文本框位於另一個驗證組上。ASP.NET:當我點擊一個LinkBut​​ton

這兩個列表都有一個OnSelectedIndexChanged事件處理程序,它根據DropDownLists中的選擇進行一些計算。這兩份清單也有AutoPostBack="True"

該按鈕具有OnClick處理程序,其也不會根據在文本框中的數量一些計算。

我需要的計算立即更新 - 所以我加了另一個數據爲Repeater結合 - 在每個事件處理程序

問題是 - 我點擊後,我無法從文本框中的值在其中一個DropDownLists。它總是脫落爲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 

最後,LinkButtonOnClick處理:

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 

謝謝:)

回答

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 
0

我知道這已經回答了,但我有一個非常類似的問題,有一個完全不同的解決方案,因爲這是第一篇文章中出現的谷歌,我想我會在這裏添加它。

我有一個鏈接按鈕,當它點擊它,但只在IE9中,它似乎激活頁面上的另一個按鈕,並將其稱爲事件而不是它自己的。

經過大量的搜索和搜索之後,我意識到,如果我在頁面的特定部分點擊任何地方,我不僅僅激活了其他按鈕。

我最終在完全剝離我的頁面後發現問題。它是這樣的:

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

空標籤標籤。一旦刪除一切都很好。

相關問題