UserControl 中嵌套控件的 FindControl 返回 null
我有一个非常奇怪的问题。我有一个 UserControl,里面有一些控件。我想在另一个回发中引用这些控件。但是当我尝试获取它们时,控件的 Controls
属性返回 null。 我正在使用vs2008。
下面是示例代码:
public partial class MyUserControl : System.Web.UI.UserControl, INamingContainer
{
protected void Page_Load(object sender, EventArgs e)
{
foreach (Control control in this.Controls)
{
Response.Write(control.ClientID);
}
}
private void MyTable()
{
Table table = new Table();
TableRow row = new TableRow();
TableCell cell = new TableCell();
CheckBox check = new CheckBox();
check.ID = "theId";
check.Text = "My Check";
check.AutoPostBack = true;
cell.Controls.Add(check);
row.Cells.Add(cell);
check = new CheckBox();
check.ID = "theOther";
check.AutoPostBack = true;
check.Text = "My Other Check";
cell = new TableCell();
cell.Controls.Add(check);
row.Cells.Add(cell);
table.Rows.Add(row);
this.Controls.Add(table);
}
protected override void Render(HtmlTextWriter writer)
{
MyTable();
base.Render(writer);
}
}
Default.aspx 页面类似于:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.cs" Inherits="Tester.Default" %>
<%@ Register TagPrefix="uc1" TagName="MyControl" Src="~/MyUserControl.ascx" %>
<!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>Unbenannte Seite</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:MyControl ID="MyControlInstance" runat="server" />
</div>
</form>
</body>
</html>
我不知道我是否迷失在 ASP.NET 生命周期的某些部分中。但这种情况让我抓狂。任何帮助将非常感激。
I have a very weird issue. I have a UserControl that has some controls inside. I want to refer those controls after, in another postback. But when I try to get them the Controls
property of my controls returns null.
I'm working on vs2008.
Here is the sample code:
public partial class MyUserControl : System.Web.UI.UserControl, INamingContainer
{
protected void Page_Load(object sender, EventArgs e)
{
foreach (Control control in this.Controls)
{
Response.Write(control.ClientID);
}
}
private void MyTable()
{
Table table = new Table();
TableRow row = new TableRow();
TableCell cell = new TableCell();
CheckBox check = new CheckBox();
check.ID = "theId";
check.Text = "My Check";
check.AutoPostBack = true;
cell.Controls.Add(check);
row.Cells.Add(cell);
check = new CheckBox();
check.ID = "theOther";
check.AutoPostBack = true;
check.Text = "My Other Check";
cell = new TableCell();
cell.Controls.Add(check);
row.Cells.Add(cell);
table.Rows.Add(row);
this.Controls.Add(table);
}
protected override void Render(HtmlTextWriter writer)
{
MyTable();
base.Render(writer);
}
}
and the Default.aspx page is something like:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.cs" Inherits="Tester.Default" %>
<%@ Register TagPrefix="uc1" TagName="MyControl" Src="~/MyUserControl.ascx" %>
<!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>Unbenannte Seite</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:MyControl ID="MyControlInstance" runat="server" />
</div>
</form>
</body>
</html>
I don't know if I'm lost in some part of the ASP.NET life cycle. But this situation is making me crazy. Any help would be very grateful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
CreateChildControls
或OnInit
中创建子控件 (MyTable
):或者
您不应该/不能在
Render
中创建控件code> 发生在Page_Load
之后。请参阅此处了解 ASP.Net 页面生命周期。Create your child controls (
MyTable
) in eitherCreateChildControls
orOnInit
:Or
You shouldn't/cannot create controls in
Render
as it occurs afterPage_Load
. See the ASP.Net Page Lifecycle here.我相信这是因为
Render
事件发生在Page_Load
之后,因此当您尝试迭代控件集合时,它尚未设置。最常见的解决方案是重写 CreateChildControls 以获得正确的计时。I believe it is because the
Render
event occurs afterPage_Load
, so when you are trying to iterate your control collection, it hasn't been set up yet. Most common solution is to overrideCreateChildControls
to get the proper timing down.