gpt4 book ai didi

asp.net - UpdatePanel中的按钮随机丢失事件处理程序

转载 作者:行者123 更新时间:2023-12-04 04:41:53 25 4
gpt4 key购买 nike

在ASP.NET网页上,我有一个UpdatePanel。在此UpdatePanel内部,我有一个“添加”按钮,该按钮可将我创建的任意数量的用户控件动态添加到PlaceHolder中。用户控件包含标签,按钮和第三方控件。用户控件内的按钮用于从父面板中删除特定的用户控件。 “添加”和“删除”功能均正常工作。但是,“添加”按钮上的事件处理程序有时会停止工作,没有明显的模式或原因。如果我添加了2个UC并删除了1个,则“添加”按钮有时会继续起作用,有时却不会。找出原因的任何步骤都是有帮助的。

对于上下文,UC被称为“Whereas”,因为它们是决议中的WHEREAS子句。这是代码,仅缩减至相关内容:

Edit.aspx

...
<asp:ScriptManager ID="sm1" runat="server"></asp:ScriptManager>
<asp:Panel ID="pnlMain" runat="server">
<div class="form-horizontal">
<asp:UpdatePanel ID="upnlWhereas" runat="server">
<ContentTemplate>
<asp:PlaceHolder ID="phWhereas" runat="server"></asp:PlaceHolder>
<div class="control-group">
<asp:Label ID="lblAddWhereas" AssociatedControlID="btnAdd1Whereas" CssClass="control-label" runat="server"></asp:Label>
<div class="controls">
<asp:Button ID="btnAdd1Whereas" CssClass="btn" UseSubmitBehavior="false" Text="Add Whereas" runat="server" />
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</asp:Panel>
...

Edit.aspx.vb (类为Edit2)
...
Private Property ControlsList As List(Of String)
Get
If ViewState("ControlsList") Is Nothing Then
ViewState("ControlsList") = New List(Of String)
End If

Return CType(ViewState("ControlsList"), List(Of String))
End Get
Set(ByVal Value As List(Of String))
ViewState("ControlsList") = Value
End Set
End Property

Private ReadOnly Property NextID As Integer
Get
Return ControlsList.Count + 1
End Get
End Property

Protected Overrides Sub LoadViewState(savedState As Object)
MyBase.LoadViewState(savedState)

For Each waID As String In ControlsList
Dim NewWhereas As Whereas
NewWhereas = Me.LoadControl("~/UserControls/Whereas.ascx")

NewWhereas.ID = waID
phWhereas.Controls.Add(NewWhereas)

AddHandler NewWhereas.OnRemoveWhereasClick, AddressOf Whereas_OnRemoveWhereasClick
Next
End Sub

...

Private Sub btnAdd1Whereas_Click(sender As Object, e As System.EventArgs) Handles btnAdd1Whereas.Click
Dim NewWhereas As Whereas
NewWhereas = Me.LoadControl("~/UserControls/Whereas.ascx")

NewWhereas.ID = "wa" + NextID.ToString
phWhereas.Controls.Add(NewWhereas)

ControlsList.Add(NewWhereas.ID)
End Sub

Private Sub Whereas_OnRemoveWhereasClick(ByVal sender As Object)
Dim thisButton As Button = sender
Dim thisWhereas As Whereas = thisButton.Parent.Parent

phWhereas.Controls.Remove(thisWhereas)
ControlsList.Remove(thisWhereas.ID)
End Sub
...

,而asasx.vb
Public Delegate Sub ActionClick(ByVal sender As Object)

Public Class Whereas
Inherits System.Web.UI.UserControl

...

Public Event OnRemoveWhereasClick As ActionClick

Public Sub btnRemoveWhereas_Click(ByVal sender As Object, ByVal e As EventArgs)
RaiseEvent OnRemvoeWhereasClick(sender)
End Sub
End Class

最佳答案

当文档大纲更改时,“更新”面板的行为通常会很奇怪。
尝试将“添加”按钮移到面板外,然后在更新面板的“触发器”部分中添加按钮。

关于asp.net - UpdatePanel中的按钮随机丢失事件处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14610384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com