473,387 Members | 1,534 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,387 software developers and data experts.

why Page.LoadControl?

hi,

I create a UserControl called "RegInfo.ascx",
then i add a PlaceHolder to WebForm1,
and in WebForm1.Page_Load() i wrote the follow statement:
placeHolder.Controls.Add(new RegInfo());
but when i pressed F5 to run the web app, it shows nothing. :(

after search web to find out solution, i replaced upper statement with:
placeHolder.Controls.Add(Page.LoadControl("RegInfo .ascx"));
it work as my expect!

I am so confused about the two results,
Don't they add a new instance of RegInfo into the placeHolder?
Why one can work exactly while anthor don't?
Nov 19 '05 #1
5 1732
When you load a control, you do more than instantiate it..

new RegInfo();, as you know, creates a new instance of a class.

Page.LoadControl also creates a new instance, but also does a lot
more...such as bringing the control up to the same event life cycle. AS you
can imagine, if new did this, it would be inconsitent with how new works for
normal classes (simply calling the constructor...).

Cheers,
Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"alecyy" <ci*******@163.com> wrote in message
news:OX**************@TK2MSFTNGP15.phx.gbl...
hi,

I create a UserControl called "RegInfo.ascx",
then i add a PlaceHolder to WebForm1,
and in WebForm1.Page_Load() i wrote the follow statement:
placeHolder.Controls.Add(new RegInfo());
but when i pressed F5 to run the web app, it shows nothing. :(

after search web to find out solution, i replaced upper statement with:
placeHolder.Controls.Add(Page.LoadControl("RegInfo .ascx"));
it work as my expect!

I am so confused about the two results,
Don't they add a new instance of RegInfo into the placeHolder?
Why one can work exactly while anthor don't?

Nov 19 '05 #2
when you do new RegInfo() you are only creating an instance of the code
behind of your user control, which normally doesn't have any code to create
the controls it contains (these are usually on the aspx page that get
compiled). Page.LoadControl("RegInfo.ascx") creates an instance of the
actual user control (which inhertis from the code behind).

-- bruce (sqlwork.com)
"alecyy" <ci*******@163.com> wrote in message
news:OX**************@TK2MSFTNGP15.phx.gbl...
hi,

I create a UserControl called "RegInfo.ascx",
then i add a PlaceHolder to WebForm1,
and in WebForm1.Page_Load() i wrote the follow statement:
placeHolder.Controls.Add(new RegInfo());
but when i pressed F5 to run the web app, it shows nothing. :(

after search web to find out solution, i replaced upper statement with:
placeHolder.Controls.Add(Page.LoadControl("RegInfo .ascx"));
it work as my expect!

I am so confused about the two results,
Don't they add a new instance of RegInfo into the placeHolder?
Why one can work exactly while anthor don't?

Nov 19 '05 #3
when you do new RegInfo() you are only creating an instance of the code
behind of your user control, which normally doesn't have any code to create
the controls it contains (these are usually on the aspx page that get
compiled). Page.LoadControl("RegInfo.ascx") creates an instance of the
actual user control (which inhertis from the code behind).

-- bruce (sqlwork.com)
"alecyy" <ci*******@163.com> wrote in message
news:OX**************@TK2MSFTNGP15.phx.gbl...
hi,

I create a UserControl called "RegInfo.ascx",
then i add a PlaceHolder to WebForm1,
and in WebForm1.Page_Load() i wrote the follow statement:
placeHolder.Controls.Add(new RegInfo());
but when i pressed F5 to run the web app, it shows nothing. :(

after search web to find out solution, i replaced upper statement with:
placeHolder.Controls.Add(Page.LoadControl("RegInfo .ascx"));
it work as my expect!

I am so confused about the two results,
Don't they add a new instance of RegInfo into the placeHolder?
Why one can work exactly while anthor don't?

Nov 19 '05 #4
thx.
i got it.

"Bruce Barker" <br******************@safeco.com> дÈëÏûÏ¢
news:#Z*************@TK2MSFTNGP12.phx.gbl...
when you do new RegInfo() you are only creating an instance of the code
behind of your user control, which normally doesn't have any code to create the controls it contains (these are usually on the aspx page that get
compiled). Page.LoadControl("RegInfo.ascx") creates an instance of the
actual user control (which inhertis from the code behind).

-- bruce (sqlwork.com)
"alecyy" <ci*******@163.com> wrote in message
news:OX**************@TK2MSFTNGP15.phx.gbl...
hi,

I create a UserControl called "RegInfo.ascx",
then i add a PlaceHolder to WebForm1,
and in WebForm1.Page_Load() i wrote the follow statement:
placeHolder.Controls.Add(new RegInfo());
but when i pressed F5 to run the web app, it shows nothing. :(

after search web to find out solution, i replaced upper statement with:
placeHolder.Controls.Add(Page.LoadControl("RegInfo .ascx"));
it work as my expect!

I am so confused about the two results,
Don't they add a new instance of RegInfo into the placeHolder?
Why one can work exactly while anthor don't?


Nov 19 '05 #5
thx.
i got it.

"Bruce Barker" <br******************@safeco.com> дÈëÏûÏ¢
news:#Z*************@TK2MSFTNGP12.phx.gbl...
when you do new RegInfo() you are only creating an instance of the code
behind of your user control, which normally doesn't have any code to create the controls it contains (these are usually on the aspx page that get
compiled). Page.LoadControl("RegInfo.ascx") creates an instance of the
actual user control (which inhertis from the code behind).

-- bruce (sqlwork.com)
"alecyy" <ci*******@163.com> wrote in message
news:OX**************@TK2MSFTNGP15.phx.gbl...
hi,

I create a UserControl called "RegInfo.ascx",
then i add a PlaceHolder to WebForm1,
and in WebForm1.Page_Load() i wrote the follow statement:
placeHolder.Controls.Add(new RegInfo());
but when i pressed F5 to run the web app, it shows nothing. :(

after search web to find out solution, i replaced upper statement with:
placeHolder.Controls.Add(Page.LoadControl("RegInfo .ascx"));
it work as my expect!

I am so confused about the two results,
Don't they add a new instance of RegInfo into the placeHolder?
Why one can work exactly while anthor don't?


Nov 19 '05 #6

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

Similar topics

3
by: Tom | last post by:
Hi, I have an index.aspx page which includes top.aspx, left.aspx, main.aspx and bottom.aspx. In the left.aspx, there is a login web control - login.ascx. It keeps session of username and role...
6
by: martin | last post by:
Hi, I am a web page and a web user control. My web user control is placed in my web page using the following directive <%@ Register TagPrefix="uc1" TagName="Header"...
4
by: Christopher | last post by:
I am trying to load a UserControl which inherits from one of our customer classes which in turn inherits from UserControl. We use Page.LoadControl() from within our ASP.NET pages to load these...
4
by: Support | last post by:
Hello: I have a aspx web page - let's call it main.aspx It contains a placeholder: Myplaceholder. I have a web control, lets call it webcontrol which has a code behind class called...
5
by: Jay Douglas | last post by:
I have a set of pages that inherit from a base class in the App_Code folder. The class looks something like: public class MyBaseClass : System.Web.UI.Page In various stages of the life cycle I...
1
by: Joe | last post by:
Hello All, I have a class that inherits from System.Web.UI.Page. In it I call the Page.LoadControl. I am having trouble with the virtual path that the LoadControl method accepts as its...
1
by: RSH | last post by:
Hi, I have a situation where I have a user Control (ResultHeader.ascx) that has a button. I am placing that control into a page (samplePage.aspx). I have setup a custom event, that should from...
1
by: pyrusmagnus | last post by:
Hi, I'm having trouble loading a user control from inside my Sharepoint webpart. A System.IO.FileNotFoundException occurs on this line: Control myControl = Page.LoadControl("/UserControls/...
2
by: =?Utf-8?B?SlQtQVJM?= | last post by:
I have a pretty tricky problem. I have a dynamically built page. I use Page.ParseControls to parse the "entire page" (ASPX file contains only a Page tag). ParseControls is pretty resource...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.