473,396 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

OnInit / User Controls

Why is it that when I override the OnInit method for a page, I can add a
user control like this:

Me.Controls.Add(LoadControl("MyControl.ascx"))

(where MyControl.ascx is your every day user control)

.... but when I use:

Dim mC As New MyControl
Me.Controls.Add(mC)

.... it doesn't show up?

It works if use the generic Control type:

Dim mC As Control
mC = LoadControl("MyControl.ascx")
Me.Controls.Add(mC)

I thought ASP.NET would add user controls to a page just the same as
controls... am I missing something?

Brandon
Nov 18 '05 #1
2 5268
Hi,

it is the difference between code-behind and inline code and its impact what
you see here. Code-behind model in v1 and v1.1 involves two classes, the one
that is compiled dynamically (by page parser) and then the actual
code-behind class (which you develop with VS.NET). The dynamically created
class inherits from your code-behind class, that's the idea briefly.

Now when you instantiate the control like: Dim mC As New MyControl, you
instantiate the code-behind class but you don't cause parsing happen for the
ascx (which contains the markup). E.g you create control instance of the
code-behind class but not the dynamic class (which is created when ascx is
parsed) and the code-behind class lacks the markup which is in dynamic
class.

When you create the control instance with LoadControl, ascx is parsed and
the ascx file contains details about the relevant code-behind class (in
directive) and therefore the correct code-behind class is instantiated also.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke

"Brandon Potter" <msnews@brandonpotter_nospam.com> wrote in message
news:uC**************@TK2MSFTNGP10.phx.gbl...
Why is it that when I override the OnInit method for a page, I can add a
user control like this:

Me.Controls.Add(LoadControl("MyControl.ascx"))

(where MyControl.ascx is your every day user control)

... but when I use:

Dim mC As New MyControl
Me.Controls.Add(mC)

... it doesn't show up?

It works if use the generic Control type:

Dim mC As Control
mC = LoadControl("MyControl.ascx")
Me.Controls.Add(mC)

I thought ASP.NET would add user controls to a page just the same as
controls... am I missing something?

Brandon

Nov 18 '05 #2
Hi Brandon:

Dim mC As New MyControl

Creates an object of type MyControl - which is the class "behind" the
ascx.

LoadControl("MyControl.ascx")

Goes through the additional steps needed to compile the ascx,
initialize it, automatically wire up event handlers if needed, etc.
etc.

The difference then isn't in how you declare the variable (MyControl
versus a plain Control), but how it's created (with New or with
LoadControl). The following should work if you need a variable of type
MyControl.

Dim mC As MyControl
mC = DirectCast(LoadControl("MyControl.ascx"), MyControl)
Me.Controls.Add(mC)

HTH,

--
Scott
http://www.OdeToCode.com

On Fri, 9 Jul 2004 11:26:52 -0400, "Brandon Potter"
<msnews@brandonpotter_nospam.com> wrote:
Why is it that when I override the OnInit method for a page, I can add a
user control like this:

Me.Controls.Add(LoadControl("MyControl.ascx"))

(where MyControl.ascx is your every day user control)

... but when I use:

Dim mC As New MyControl
Me.Controls.Add(mC)

... it doesn't show up?

It works if use the generic Control type:

Dim mC As Control
mC = LoadControl("MyControl.ascx")
Me.Controls.Add(mC)

I thought ASP.NET would add user controls to a page just the same as
controls... am I missing something?

Brandon


Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: poi | last post by:
I define controls completely in code in a PrepControls() method. I put the method in here: #region Web Form Designer generated code override protected void OnInit(EventArgs e) {...
8
by: Invalidlastname | last post by:
Hi, We are developing an asp.net application, and we dynamically created certain literal controls to represent some read-only text for certain editable controls. However, recently we found an issue...
4
by: dx | last post by:
If not is it appropriate to place an EnsureChildControls within an objects OnInit method? TIA
1
by: Sally | last post by:
Just confused about when OnInit and Constructor gets called in the parent/child case. Page.Constructor() Page.AddParsedSubObject() <- do all the sub's constructors get called here?...
1
by: Neil Zanella | last post by:
Hello, I would like to ask a question pertaining to programmatic initialization of ASP web controls (as opposed to initialization via standalone attributes and other elements contained in the...
0
by: multiformity | last post by:
Ok, so I have gone off and documented the lifecycle of a page with a custom composite control on it. You can find that document here: http://www.ats-engineers.com/lifecycle.htm Now, I am...
3
by: Beavis | last post by:
I hate to repost a message, but I am still at the same point where I was when I originally posted, and hopefully someone else will see this one... Ok, so I have gone off and documented the...
4
by: =?Utf-8?B?TWlrZSBDb2xsaW5z?= | last post by:
In my web application, I have a treeview. Based on which node is clicked, I am loading user controls. Currently, I am loading my controls in the SelectedNodeChanged event, but I want to load the...
0
by: bruce barker | last post by:
while OnInit is fine, the proper place is the CreateChildControls method. -- bruce (sqlwork.com) Richard Coltrane wrote:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
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,...

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.