468,537 Members | 2,419 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,537 developers. It's quick & easy.

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

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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Sep 5 '07 #1
7 1758
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
}

Sep 5 '07 #2
use OnInit. the controls defined in the aspx file are not created in the
constructor, but during an initialization event.

-- bruce (sqlwork.com)

Nathan Sokalski wrote:
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.
Sep 5 '07 #3
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);
--
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage

"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:et**************@TK2MSFTNGP05.phx.gbl...
>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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

Sep 5 '07 #4
If I were to use the Page.LoadControl() method, how can I pass parameters
like I do with New()?
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Mark Fitzpatrick" <ma******@fitzme.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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);
--
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage

"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:et**************@TK2MSFTNGP05.phx.gbl...
>>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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/


Sep 5 '07 #5
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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Muhammad Naveed Yaseen" <mn******@gmail.comwrote in message
news:11*********************@57g2000hsv.googlegrou ps.com...
>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
}

Sep 5 '07 #6
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
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"bruce barker" <no****@nospam.comwrote in message
news:uy*************@TK2MSFTNGP06.phx.gbl...
use OnInit. the controls defined in the aspx file are not created in the
constructor, but during an initialization event.

-- bruce (sqlwork.com)

Nathan Sokalski wrote:
>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.

Sep 5 '07 #7
Define the needed things as properties on the control, which you set when
you have the instance at hand.

--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net

"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
If I were to use the Page.LoadControl() method, how can I pass parameters
like I do with New()?
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Mark Fitzpatrick" <ma******@fitzme.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>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 );
--
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage

"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:et**************@TK2MSFTNGP05.phx.gbl...
>>>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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/



Sep 5 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Ludovic SOEUR | last post: by
6 posts views Thread by Earl Teigrob | last post: by
2 posts views Thread by Sam Kuehn | last post: by
8 posts views Thread by mark.norgate | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.