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

  • Thread starter Nathan Sokalski
  • 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.
 
M

Muhammad Naveed Yaseen

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
}
 
B

bruce barker

use OnInit. the controls defined in the aspx file are not created in the
constructor, but during an initialization event.

-- bruce (sqlwork.com)
 
M

Mark Fitzpatrick

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);
 
N

Nathan Sokalski

If I were to use the Page.LoadControl() method, how can I pass parameters
like I do with New()?
 
N

Nathan Sokalski

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.
 
N

Nathan Sokalski

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
 
T

Teemu Keiski

Define the needed things as properties on the control, which you set when
you have the instance at hand.
 

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,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top