Webcontrols are NULL under certain circumstances in User Controls

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>
<asp:panel ID="Panel1" runat="server" Height="50px"
Width="125px">
</asp:panel>
</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
 
K

Karl Seguin

Yes and no. It is a bug, but not in ASP.NET as you imply, it's in your code
:p

User controls are instantiated via new, they are created via
Page.LoadControl("~/controls/usercontrol.ascx");, in your case:

TestControl tc = (TestControl) Page.LoadControl("TestControl.acsx")

this is because unlike other classes, they are templated and require this
type of creation.

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top