UserControl 中嵌套控件的 FindControl 返回 null

发布于 2024-09-16 20:40:15 字数 1989 浏览 1 评论 0原文

我有一个非常奇怪的问题。我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请爱~陌生人 2024-09-23 20:40:15

CreateChildControlsOnInit 中创建子控件 (MyTable):

protected override void CreateChildControls()
{
    MyTable();
    base.CreateChildControls();
}

或者

protected override void OnInit(object sender, EventArgs e) 
{
    MyTable();
    base.OnInit(e);
}

您不应该/不能在 Render 中创建控件code> 发生在 Page_Load 之后。请参阅此处了解 ASP.Net 页面生命周期。

Create your child controls (MyTable) in either CreateChildControls or OnInit:

protected override void CreateChildControls()
{
    MyTable();
    base.CreateChildControls();
}

Or

protected override void OnInit(object sender, EventArgs e) 
{
    MyTable();
    base.OnInit(e);
}

You shouldn't/cannot create controls in Render as it occurs after Page_Load. See the ASP.Net Page Lifecycle here.

小嗷兮 2024-09-23 20:40:15

我相信这是因为 Render 事件发生在 Page_Load 之后,因此当您尝试迭代控件集合时,它尚未设置。最常见的解决方案是重写 CreateChildControls 以获得正确的计时。

I believe it is because the Render event occurs after Page_Load, so when you are trying to iterate your control collection, it hasn't been set up yet. Most common solution is to override CreateChildControls to get the proper timing down.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文