[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.aspnet.buildingcontrols

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

Nathan Sokalski

9/5/2007 2:47:00 AM

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
njsokalski@hotmail.com
http://www.nathansok...


8 Answers

Muhammad Naveed Yaseen

9/5/2007 3:40:00 AM

0

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

unknown

9/5/2007 3:44:00 AM

0

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.

Mark Fitzpatrick

9/5/2007 2:01:00 PM

0

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" <njsokalski@hotmail.com> wrote in message
news:etnqFc27HHA.5796@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
> njsokalski@hotmail.com
> http://www.nathansok...
>


Nathan Sokalski

9/5/2007 3:13:00 PM

0

If I were to use the Page.LoadControl() method, how can I pass parameters
like I do with New()?
--
Nathan Sokalski
njsokalski@hotmail.com
http://www.nathansok...

"Mark Fitzpatrick" <markfitz@fitzme.com> wrote in message
news:%23n$7pU87HHA.4660@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" <njsokalski@hotmail.com> wrote in message
> news:etnqFc27HHA.5796@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
>> njsokalski@hotmail.com
>> http://www.nathansok...
>>
>
>


Nathan Sokalski

9/5/2007 3:26:00 PM

0

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
njsokalski@hotmail.com
http://www.nathansok...

"Muhammad Naveed Yaseen" <mnyaseen@gmail.com> wrote in message
news:1188963624.668839.75860@57g2000hsv.googlegroups.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
> }
>


Nathan Sokalski

9/5/2007 3:36:00 PM

0

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
njsokalski@hotmail.com
http://www.nathansok...

"bruce barker" <nospam@nospam.com> wrote in message
news:uytLd827HHA.980@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.


Teemu Keiski

9/5/2007 7:09:00 PM

0

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....
http://teemu...

"Nathan Sokalski" <njsokalski@hotmail.com> wrote in message
news:%23$CYC987HHA.3940@TK2MSFTNGP05.phx.gbl...
> If I were to use the Page.LoadControl() method, how can I pass parameters
> like I do with New()?
> --
> Nathan Sokalski
> njsokalski@hotmail.com
> http://www.nathansok...
>
> "Mark Fitzpatrick" <markfitz@fitzme.com> wrote in message
> news:%23n$7pU87HHA.4660@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" <njsokalski@hotmail.com> wrote in message
>> news:etnqFc27HHA.5796@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
>>> njsokalski@hotmail.com
>>> http://www.nathansok...
>>>
>>
>>
>
>


Mulligan

12/2/2011 8:27:00 PM

0

On Dec 2, 3:20 pm, amalthe...@yahoo.com wrote:
> On Dec 2, 10:00 am, Mulligan <t2judgm...@gmail.com> wrote:
>
>
>
>
>
> > On Dec 1, 5:41 pm, amalthe...@yahoo.com wrote:
>
> > > On Dec 1, 10:21 am, Mulligan <t2judgm...@gmail.com> wrote:
>
> > > > On Dec 1, 11:28 am, amalthe...@yahoo.com wrote:
>
> > > > > On Dec 1, 4:13 am, Mulligan <t2judgm...@gmail.com> wrote:
>
> > > > > > On Nov 30, 4:32 pm, amalthe...@yahoo.com wrote:
>
> > > > > > > On Nov 30, 8:06 am, Mulligan <t2judgm...@gmail.com> wrote:
>
> > > > > > > > On Nov 30, 12:28 pm, amalthe...@yahoo.com wrote:
>
> > > > > > > > > alt. idiot. mulligan
>
> > > > > > > > I think all these newsgroups should be changed to
> > > > > > > > bigcrybabyrobbieandhissockpuppetsdestroyingtheusenetinterfacebecauseheisabi­­­­­­­­gcrybabyloser.com
>
> > > > > > > You surprised me, Mulligan, I thought you would answer with a 40 I.Q.
> > > > > > > statement but instead your response was easily 60 I.Q.l
>
> > > > > > Too bad your IQ - if that was my IQ as you state above without
> > > > > > qualification - would be -35.2.
>
> > > > > Your above response is idiot typical
>
> > > > Since I am not an idiot anything that follows afterward typed by you
> > > > is totally meaningless and irrelevant.- Hide quoted text -
>
> > > > - Show quoted text -
>
> > > Every idiot claims not to be an idiot,...if you make idiot statements,
> > > then you are an idiot.  Live with it!- Hide quoted text -
>
> > How is my naming a newsgroup to match your childish made up newsgroup
> > - IOW me clowning you - make you somehow possessing the higher moral
> > superiority?-
>
> You just proved my claim.

That you use sockpuppets to hide behind on usenet because you are a
coward who feels his one voice is so meaningless he has to try to dupe
others in a group to think the way he thinks they should?