Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net UserControl中嵌套控件的FindControl返回null_Asp.net_User Controls_Findcontrol - Fatal编程技术网

Asp.net UserControl中嵌套控件的FindControl返回null

Asp.net UserControl中嵌套控件的FindControl返回null,asp.net,user-controls,findcontrol,Asp.net,User Controls,Findcontrol,我有一个很奇怪的问题。我有一个UserControl,里面有一些控件。我想在后面的另一个回发中引用这些控件。但是当我尝试获取它们时,我的控件的控件属性返回null。 我正在做vs2008 以下是示例代码: public partial class MyUserControl : System.Web.UI.UserControl, INamingContainer { protected void Page_Load(object sender, EventArgs e) {

我有一个很奇怪的问题。我有一个UserControl,里面有一些控件。我想在后面的另一个回发中引用这些控件。但是当我尝试获取它们时,我的控件的
控件
属性返回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);
    }
}
默认的.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生命周期的某个部分。但这种情况让我抓狂。任何帮助都将不胜感激。

CreateChildControls
OnInit
中创建您的子控件(
MyTable
):

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


您不应该/不能在
Render
中创建控件,因为它发生在
Page\u Load
之后。请参阅ASP.Net页面生命周期。

CreateChildControls
OnInit
中创建子控件(
MyTable
):

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


您不应该/不能在
Render
中创建控件,因为它发生在
Page\u Load
之后。查看ASP.Net页面生命周期。

我认为这是因为
呈现事件发生在
页面加载之后,因此当您尝试迭代控件集合时,它尚未设置。最常见的解决方案是重写
CreateChildControls
,以获得正确的计时。我认为这是因为
Render
事件发生在
Page\u Load
之后,因此当您尝试迭代控件集合时,它尚未设置。最常见的解决方案是重写
CreateChildControls
,以获得正确的定时。

你说得对。现在我想问你。每次页面回发时都应该创建表?或者有一种方法可以将它保存在内存中,以便以后处理。你应该每次都重新创建它。你是对的。现在我想问你。每次页面回发时都应该创建表?或者有一种方法可以将它保存在内存中,以便以后处理。您应该每次都重新创建它。