将控件添加到面板会导致“对象引用未设置为对象的实例”错误
尝试动态添加动态生成内容的用户控件。用户控件无法获取面板上的句柄来放入控件。
首先我有一个页面(test.aspx):
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</form>
</body>
</html>
后面的代码:
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TestUserControl uc = new TestUserControl();
Panel1.Controls.Add(uc);
//this is where the error happens:
uc.Fill();
}
}
然后这是用户控件:
<asp:Panel ID="pnlTest" runat="server" >
</asp:Panel>
后面的代码:
public partial class TestUserControl: System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void Fill()
{
Label lbl = new Label();
lbl.Text = "test";
//This is where pnlTest is null and I get "Object Reference Not Found..."
pnlTest.Controls.Add(lbl);
}
}
所以...看起来像我调用 Fill() 的时间点是在呈现用户控件之前,因此 pnlTest 尚未实例化。但是,我不确定在哪里从 test.aspx 调用 uc.Fill() ...我尝试了 Page_PreRenderComplete,但这也不起作用...
(如果您看到名称不匹配...那可能不是错误......名字已被更改以保护无辜者)
Trying to dynamically add a user control that dynamically generates content. The User Control cannot get a handle on the panel to put controls in.
First I have a page (test.aspx):
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</form>
</body>
</html>
code behind:
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TestUserControl uc = new TestUserControl();
Panel1.Controls.Add(uc);
//this is where the error happens:
uc.Fill();
}
}
and then here is the user control:
<asp:Panel ID="pnlTest" runat="server" >
</asp:Panel>
and the code behind:
public partial class TestUserControl: System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void Fill()
{
Label lbl = new Label();
lbl.Text = "test";
//This is where pnlTest is null and I get "Object Reference Not Found..."
pnlTest.Controls.Add(lbl);
}
}
So... It seems like the point at which I'm calling Fill() is before the user control has been rendered, therefore pnlTest has not been instantiated. But, I'm not sure where to call uc.Fill() from test.aspx ... I tried Page_PreRenderComplete, but that didn't work either...
(if you see a name mis-match.. that's probably not the error... the names have been changed to protect the innocent)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
万岁,我可以回答我自己的问题。
我将 test.aspx.cs 更改为:
hurray, I can answer my own question.
I changed test.aspx.cs to this:
我有类似的问题。事实证明,系统找不到对该对象的引用,因为该对象尚未结构化/包含在
[filename].aspx.designer.cs
文件中。I had a similar issue. Turns out the system could not find a reference to the object because it had not been structured/included in the
[filename].aspx.designer.cs
file.Windows 窗体应用程序中存在类似问题 - 需要添加
到表单代码中。即使项目有对 DLL 的引用,该引用也需要位于表单代码中。
Similar issue in Windows Forms app - Needed to add
to the form code. Even though the project had a reference to the DLL, the ref needed to be in the form code.