473,498 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1869
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2178
by: Jose Michael Meo R. Barrido | last post by:
I made a custom runded rectangle usercontrol. I used a function i found on the internet. the function works fine(see "GetRoundRect" below). I use the fullowing code to make my usercontrol...
2
1571
by: Ludovic SOEUR | last post by:
Have everyone tried to create controls in separated threads ? I have a problem that I do not understand. To simplify the explanations, I wrote theses few lines to show an example of the problem....
6
2538
by: Earl Teigrob | last post by:
I am writing an application that dynamically loads user controls at run time based on user options. I would like to give my users the ability to build their own user controls and add them to my...
1
2405
by: Jeff Smith | last post by:
Can I load custom web user controls dynamically and access the properties and methods without having to explicitly define custom control types (example 2 below). I have custom web control named...
2
2010
by: Sam Kuehn | last post by:
There has been a lot of articles on how to load user controls at runtime in the Init() method. UserControl myControl = (UserControl)LoadControl(stringControl); I add the control in the Init()...
2
1623
by: Joanne | last post by:
Hi, I have an "interesting" problem with UserControls in a datagrid and I'm desperate for your help as I work alone and have no-one else to ask. It is quite complicated but I'll try to keep it...
5
1704
by: snesbit | last post by:
If a screen is made up of several user controls and those user controls contain various packaged or standard controls such as a grid, how do you raise both standard and custom events from the user...
8
3154
by: mark.norgate | last post by:
I've run into a few problems trying to use generics for user controls (classes derived from UserControl). I'm using the Web Application model rather than the Web Site model. The first problem...
1
8327
by: Don | last post by:
I'm getting the following exception displayed in the task list at design time for my project: "Code generation for property 'Controls' failed. Error was: 'Object reference not set to an...
0
7167
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5464
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4915
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4593
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3095
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
657
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.