Controls still Nothing when UserControl's New() method is called

  • Thread starter Thread starter Nathan Sokalski
  • Start date Start date
N

Nathan Sokalski

I have a UserControl (*.ascx) that has the following design:

<div id="divFileDir" runat="server">
<asp:Image ID="imgIcon" runat="server" CssClass="icon" Height="16px"
Width="16px"/>
<asp:LinkButton ID="lnkName" runat="server" CssClass="filedir"
CausesValidation="false"/>
<asp:Label ID="lblSize" runat="server" CssClass="size"/>
<asp:Label ID="lblDate" runat="server" CssClass="date"/>
</div>

The Set method of some of the properties I have declared in the codebehind
set certain attributes (such as Text). In the overloaded New() method, I
assign certain values to these properties, therefore calling their Set
method. This causes the following error:

Object reference not set to an instance of an object.

The Objects that are not set to an instance of an object are the Controls in
the *.ascx file. What can I do about this to allow me to set the properties
when the UserControl is created? Thanks.
 
What can I do about this to allow me to set the properties when
the UserControl is created?

Constructor is too early as it is called before any child control is
created. Child controls are safe to access in Init override like
following.

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
// here
}
 
use OnInit. the controls defined in the aspx file are not created in the
constructor, but during an initialization event.

-- bruce (sqlwork.com)
 
Are you creating the control as you would any ordinary object suchas

ControlClass myControl = new ControlClass();?

If so, you're going to get a null every time you hit one of the asp.net
controls that's in the design surface. The reason is, using the new
assignment all you are doing is creating a new instance of the class, not
instantiating the full control. The difference, using new creates the class
without the design surface of the .ascx file. To correctly add a control to
a page programatically you need to use the LoadControl method of the page
such as:

ControlClass myControl = (ControlClass)Page.LoadControl(pathtomyascxfile);
 
If I were to use the Page.LoadControl() method, how can I pass parameters
like I do with New()?
 
I have tried using the Init event (I also tried the Load event) of the
*.ascx.vb file, and I still recieve the error.

Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Init
Me.lnkName.Text = Me.iteminfovalue.Name()
Me.lblDate.Text = Me.iteminfovalue.LastWriteTime.ToShortDateString()
End Sub

What am I doing wrong here? Thanks.
 
First, this is the codebehind of an *.ascx file, not *.aspx. Therefore, the
code that sets the values for the properties should be in the *.ascx.vb
file. I have tried using the Init (and the Load) events of the *.ascx.vb,
but I still recieve the same error. What am I doing wrong? Thanks.

Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Init
Me.lnkName.Text = Me.iteminfovalue.Name()
Me.lblDate.Text = Me.iteminfovalue.LastWriteTime.ToShortDateString()
End Sub
 
Define the needed things as properties on the control, which you set when
you have the instance at hand.
 
Back
Top