在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
...
Whereas.ascx.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
答案 0 :(得分:0)
当文档大纲发生变化时,“更新”面板通常会表现得很奇怪。 尝试移动面板外部的“添加”按钮,然后在更新面板的“触发器”部分添加按钮。
答案 1 :(得分:0)
禁用UpdatePanel后(感谢Yellowfog的建议),我发现发生了以下情况:
解决方案是使用GUID而不是整数根据列表中的控件数生成动态添加的用户控件的ID。这样他们基本上保证是独一无二的。