K
kens
I've found some odd behavior in dynamically loaded user controls under
ASP.NET 2.0. Specifically, if I load a control dynamically, in the
"Page_Load" event, the code-behind for the user control is unable to
access any of the web controls on the user control, as they are null.
Specifically, I have this user control, TestControl.ascx:
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="TestControl.ascx.cs" Inherits="TestControl" %>
<asp:Literal ID="TestLiteral" runat="server"></asp:Literal>
It has the following code-behind:
public partial class TestControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
TestLiteral.Text = "This is some literal text in a test user
control.";
}
}
Now, if I reference that control in a test page (Test.aspx) like this,
everything works fine:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs"
Inherits="Test" %>
<%@ Register Src="TestControl.ascx" TagName="TestControl"
TagPrefix="uc1" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:TestControl ID="TestControl1" runat="server" />
</div>
</form>
</body>
</html>
However, I have a second test page (Test2.aspx) which attempts to load
that user control dynamically, like this:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test2.aspx.cs"
Inherits="Test" %>
<%@ Register Src="TestControl.ascx" TagName="TestControl"
TagPrefix="uc1" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<aspanel ID="Panel1" runat="server" Height="50px"
Width="125px">
</aspanel>
</div>
</form>
</body>
</html>
With the following code-behind:
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TestControl ctlTest = new TestControl();
Panel1.Controls.Add(ctlTest);
}
}
And when I execute that second test page, the control throws a
NullReferenceException on this line, i.e., TestLiteral is Null:
TestLiteral.Text = "This is some literal text in a test user control.";
I don't think that this is expected behavior. Is this a bug?
Ken Smith
ASP.NET 2.0. Specifically, if I load a control dynamically, in the
"Page_Load" event, the code-behind for the user control is unable to
access any of the web controls on the user control, as they are null.
Specifically, I have this user control, TestControl.ascx:
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="TestControl.ascx.cs" Inherits="TestControl" %>
<asp:Literal ID="TestLiteral" runat="server"></asp:Literal>
It has the following code-behind:
public partial class TestControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
TestLiteral.Text = "This is some literal text in a test user
control.";
}
}
Now, if I reference that control in a test page (Test.aspx) like this,
everything works fine:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs"
Inherits="Test" %>
<%@ Register Src="TestControl.ascx" TagName="TestControl"
TagPrefix="uc1" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:TestControl ID="TestControl1" runat="server" />
</div>
</form>
</body>
</html>
However, I have a second test page (Test2.aspx) which attempts to load
that user control dynamically, like this:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test2.aspx.cs"
Inherits="Test" %>
<%@ Register Src="TestControl.ascx" TagName="TestControl"
TagPrefix="uc1" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<aspanel ID="Panel1" runat="server" Height="50px"
Width="125px">
</aspanel>
</div>
</form>
</body>
</html>
With the following code-behind:
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TestControl ctlTest = new TestControl();
Panel1.Controls.Add(ctlTest);
}
}
And when I execute that second test page, the control throws a
NullReferenceException on this line, i.e., TestLiteral is Null:
TestLiteral.Text = "This is some literal text in a test user control.";
I don't think that this is expected behavior. Is this a bug?
Ken Smith