如何在ASP.NET PreInit事件中获得控制权?指针为null,FindControl方法返回null.
<asp:Content ID="Content2" ContentPlaceHolderID="ContentBody" runat="server"> <asp:Table ID="Table1" runat="server" Width="100%"> ..... </asp:Table> </asp:Content>
代码如下:
private void Page_PreInit(object sender,EventArgs e) { Control table = this.FindControl("Table1"); //table is null here }
因此,在this.FindControl(“Table1”)之后,表仍然为null.该页面的NamingContainer也为null.我究竟做错了什么?
更新我必须使用此事件来创建控件.如ASP.NET Page Life Cycle Overview所述,此事件应用于动态控制创建.我需要在表格中创建一个链接列表.可能还有另一种方法吗?
解决方法
在初始化控件之前触发PreInit.阅读
ASP.NET Page Life Cycle以获取更多详细信息.
Init
Raised after all controls have been initialized and any skin
settings have been applied. The Init
event of individual controls occurs
before the Init event of the page.Use this event to read or initialize control properties.