我有一个用户控件,它使用页面初始化中的以下代码动态加载到页面中。
Dim oCtl As Object
oCtl = LoadControl("~/Controls/UserControl1.ascx")
oCtl.Id = "UserControl11"
PlaceHolder1.Controls.Clear()
PlaceHolder1.Controls.Add(oCtl)
用户控件还包含一个按钮,我无法在用户控件中捕获按钮单击。
我有一个用户控件,它使用页面初始化中的以下代码动态加载到页面中。
Dim oCtl As Object
oCtl = LoadControl("~/Controls/UserControl1.ascx")
oCtl.Id = "UserControl11"
PlaceHolder1.Controls.Clear()
PlaceHolder1.Controls.Add(oCtl)
用户控件还包含一个按钮,我无法在用户控件中捕获按钮单击。
您正在做的一些事情既不需要,也可能会导致您的问题。
这些是:
这段代码对我有用:
默认.aspx
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="Test_User_Control._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server"><title></title></head>
<body>
<form id="form1" runat="server">
<asp:PlaceHolder ID="PlaceHolder1" runat="server" />
</form>
</body>
</html>
默认.aspx.vb
Partial Public Class _Default
Inherits System.Web.UI.Page
Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
Dim control As Control = LoadControl("~/UserControl1.ascx")
PlaceHolder1.Controls.Add(control)
End Sub
End Class
用户控件1.ascx
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="UserControl1.ascx.vb" Inherits="Test_User_Control.UserControl1" %>
<asp:Label ID="Label1" Text="Before Button Press" runat="server" />
<asp:Button ID="Button1" Text="Push me" runat="server" />
用户控件1.ascx.vb
Public Partial Class UserControl1
Inherits System.Web.UI.UserControl
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = "The button has been pressed!"
End Sub
End Class
在 .NET 进入页面生命周期的“回发事件处理”步骤之前,您必须确保该控件存在于页面上。由于控件是动态添加的,因此您必须确保在每次回发时重新创建该控件,以便它可以找到触发事件的控件。
确保在每次回发时加载控件 - 如果在页面回发时控件不在控件树中,ASP.NET 将不会引发按钮单击事件。
我刚刚遇到了与您类似的问题,除了在我的情况下我没有使用会话来存储控件。
就我而言,我在这一行中找到了问题:
PlaceHolder1.Controls.Clear()
我所做的是创建一个子控件并将它们添加到 Page_Init 上的父容器中,然后处理一些事件处理程序,然后在 Page_PreRender 上我再次使用更新的数据重新创建整个列表。
我在这种情况下使用的解决方案是创建一次控件集合 - 在页面周期的早期阶段。
几个问题:
最后,.Net 的样式指南特别建议不要在 oCtl 中使用任何像 o 这样的匈牙利前缀疣,您应该将其键入为控件而不是对象。
既然你问了,我会这样写你的初始化事件。我将把 Load 事件留作练习:
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
If IsPostBack AndAlso Session("ctl") IsNot Nothing Then
Dim MyControl As Control = Session("ctl")
PlaceHolder1.Controls.Add(MyControl)
End If
End Sub
我也会想出一个比“mycontrol”更好的名字,但因为我不知道这个控件会做什么。
I don't know without trying it, but what if you programatically wire up the button's event handler? For instance, in the code-behind for the User Control itself, in Init or Load (not sure):
AddHandler Button1.Click, AddressOf Button1_Click
If that doesn't do anything, I know it's less efficient, but what if you don't store the User Control instance in Session and always recreate it in Page_Init every time?
Here is the complete code for the Page
Partial Class DynamicLoad
Inherits System.Web.UI.Page
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
If IsPostBack Then
If Not (Session("ctl") Is Nothing) Then
Dim oCtl As Object
oCtl = Session("ctl")
PlaceHolder1.Controls.Add(oCtl)
End If
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim oCtl As Object
oCtl = LoadControl("~/Controls/UserControl1.ascx")
oCtl.Id = "UserControl11"
PlaceHolder1.Controls.Clear()
PlaceHolder1.Controls.Add(oCtl)
Session("ctl") = oCtl
End If
End Sub
End Class
Here is the complete code for the user Control
Partial Class UserControl1
Inherits System.Web.UI.UserControl
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = "This is Text AFTER Post Back in User Control 1"
End Sub
End Class
您只想在不是 isPostBack 时加载控件