Dynamically instantiating a statically defined custom web control

R

Rahul Mittal

I get a "System.NullReferenceException: Object reference not set to an
instance of an object" when trying to reference members of a custom web
control that is dynamically instantiated from a web form. I have included a
simple example below to demonstrate my problem.

In the example, a custom web control called ShortInput is made up of two
statically defined fields... a Label called "Caption" and a Textbox called
"Value". I try to instantiate ShortInput dynamically from within the
codebehind file Test.aspx.cs of the web form Test.aspx. I've tried this
several different ways and here's what I am getting:

1. If I instantiate the Label and Textbox in ShortInput dynamically in
ShortInput.ascx.cs instead of statically in ShortInput.ascx as shown below,
I don't get the NullReferenceException and it works fine.

2. If I instantiate ShortInput statically in Test.aspx (using @Register and
then <ucl:ShortInput ... />) instead of dynamically in Test.aspx.cs, again I
don't get the NullReferenceException and it works just fine.

3. However if I instantiate ShortInput dynamically from Test.aspx.cs, and I
instantiate the Label and Textbox in ShortInput statically as shown in my
code listing, I get a NullReferenceException when I try to reference the
Label or Textbox in ShortInput. I am guessing the problem is to do with the
fact that ShortInput.ascx is "derived" from ShortInput.ascx.cs, so if I
instantiate class ShortInput, the statically declared controls in the web
control are not instantiated with it. I am not sure how to fix this.

So, how do I correct scenario 3? The code to reproduce scenario 3 is listed
below, with the line where the exception occurs noted with ****:

---------------------------------
ShortInput.ascx.cs:

public class ShortInput : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label Caption;
protected System.Web.UI.WebControls.TextBox Value;

private void Page_Load(object sender, System.EventArgs e)
{
this.Caption.Text = "Enter value:"; // **** NullReferenceException occurs on
this line ****
}

#region Web Form Designer generated code
...
#endregion
}

---------------------------------
ShortInput.ascx:

<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="ShortInput.ascx.cs" Inherits="Rasterware.Rastersite.ShortInput"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<asp:Label id=Caption runat="server"></asp:Label>
<asp:TextBox id=Value runat="server"></asp:TextBox>

---------------------------------
Here is Test.aspx.cs where I instantiate the above control:

public class Test : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
ShortInput si = new ShortInput();
this.Controls.Add(si);
}

#region Web Form Designer generated code
...
#endregion
}

--------------------------------
Test.aspx is just the default page created by VS.NET with nothing extra
added.


Thanks for reading, and for any suggestions/assistance you might provide.

Sincerely,
Rahul Mittal
 
V

Victor Garcia Aprea [MVP]

You need to use the LoadControl method instead of the 'new' operator, i.e.:

ShortInput si = (ShortInput) LoadControl("ShortInput.ascx");

That should fix it.


--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://obies.com/vga/blog.aspx
To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
and not by private mail.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top