473,412 Members | 4,196 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,412 software developers and data experts.

I just don't get dynamic loading of controls

I just don't get this...

If I need to dynamically load controls into a web page I simply need to go PlaceHolder1.Controls.Add(new Button()); or similar. However when I need to dynamically load a web user control into a web page then if we use the same syntax it will appear to work but the web control will throw all sorts of null reference exceptions.

Rather for web user controls you have to use the syntax PlaceHolder1.Controls.Add(LoadControl("WebUserCont rol1.ascx"));

Why is it so! A couple of questions:
1. Why is there a syntactic difference between dynamically loading web controls and web user controls???? This trips me up all of the time and will be a training nightmare for my underlings.
2. Since there is a difference why does the .Add method not check for a web user control being loaded through the "new" method rather then the "LoadControl" method and throw and exception if it is being done the wrong way? (not sure if this is possible but it would really help.)
3. Has this changed in ASP.NET2?
4. LoadControl requires the path and filename of the web user control to load. Putting aside that fact that it is not strongly typed and the fact that the relative location to the web user control is a problem if the location of web pages changes, etc, etc, etc, is it possible for a web page or web user control to report their own path and file name? I have searched but did not find. If this was possible then we could go LoadControl(WebUserControl.GetAbsolutePathAndFileN ame()) which would make for much more maintanable code.
5. In response to question 4.0, has this at all changed for ASP.NET2?
Regards
Dave A
protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;
private void Page_Load(object sender, System.EventArgs e)
{
PlaceHolder1.Controls.Add(new Button());
PlaceHolder1.Controls.Add(new Label());
PlaceHolder1.Controls.Add(new DropDownList());

PlaceHolder1.Controls.Add(LoadControl("WebUserCont rol1.ascx"));
}

Dec 7 '05 #1
2 1782
Why is it so! A couple of questions:

The answer to all of your questions is that a Web User Control is a
Templated Control. The path is necessary to locate the Template.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but you can't make it stink.

"Dave A" <da**@sigmasolutionsdonotspamme.com.au> wrote in message
news:ep***************@tk2msftngp13.phx.gbl...
I just don't get this...

If I need to dynamically load controls into a web page I simply need to go
PlaceHolder1.Controls.Add(new Button()); or similar. However when I need to
dynamically load a web user control into a web page then if we use the same
syntax it will appear to work but the web control will throw all sorts of
null reference exceptions.

Rather for web user controls you have to use the syntax
PlaceHolder1.Controls.Add(LoadControl("WebUserCont rol1.ascx"));

Why is it so! A couple of questions:
1. Why is there a syntactic difference between dynamically loading web
controls and web user controls???? This trips me up all of the time and will
be a training nightmare for my underlings.
2. Since there is a difference why does the .Add method not check for a web
user control being loaded through the "new" method rather then the
"LoadControl" method and throw and exception if it is being done the wrong
way? (not sure if this is possible but it would really help.)
3. Has this changed in ASP.NET2?
4. LoadControl requires the path and filename of the web user control to
load. Putting aside that fact that it is not strongly typed and the fact
that the relative location to the web user control is a problem if the
location of web pages changes, etc, etc, etc, is it possible for a web page
or web user control to report their own path and file name? I have searched
but did not find. If this was possible then we could go
LoadControl(WebUserControl.GetAbsolutePathAndFileN ame()) which would make
for much more maintanable code.
5. In response to question 4.0, has this at all changed for ASP.NET2?
Regards
Dave A
protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;
private void Page_Load(object sender, System.EventArgs e)
{
PlaceHolder1.Controls.Add(new Button());
PlaceHolder1.Controls.Add(new Label());
PlaceHolder1.Controls.Add(new DropDownList());

PlaceHolder1.Controls.Add(LoadControl("WebUserCont rol1.ascx"));
}
Dec 7 '05 #2
Dave,
I won't disagree with you that it'd be nice if they were the same. And I can't fully answer your question (my guess is that push come to shove, they could hide the difference inside the framework and let you always use new and internally figure out what to do). But let's face it, there's a considerable difference between user controls and server controls. The most obvious is that user controls have a design-time element, while server controls don't. This is a significant difference, and surely it's obvious that the mechanism for instantiating the two must be completely different.
As for the "moving path", just use LoadControls("~/controls/WebX.ascx"); the path of the control itself will always be fixed relative to the root of the application. And if it isn't, it should be (100% of the time, I'd love an example of where it wouldn't be).

None of this has changed in 2.0.
Oh ya, don't forget, if you used new to load UserControls, you wouldn't be able to dynamically load them (you'd have to use reflection). YUCK!. Dynamically loaded user controls is super important, maybe that was a key factor in why LoadControl came to be!

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
http://openmymind.net/redirector.aspx?documentId=51 - Learn about AJAX!

"Dave A" <da**@sigmasolutionsdonotspamme.com.au> wrote in message news:ep***************@tk2msftngp13.phx.gbl...
I just don't get this...

If I need to dynamically load controls into a web page I simply need to go PlaceHolder1.Controls.Add(new Button()); or similar. However when I need to dynamically load a web user control into a web page then if we use the same syntax it will appear to work but the web control will throw all sorts of null reference exceptions.

Rather for web user controls you have to use the syntax PlaceHolder1.Controls.Add(LoadControl("WebUserCont rol1.ascx"));

Why is it so! A couple of questions:
1. Why is there a syntactic difference between dynamically loading web controls and web user controls???? This trips me up all of the time and will be a training nightmare for my underlings.
2. Since there is a difference why does the .Add method not check for a web user control being loaded through the "new" method rather then the "LoadControl" method and throw and exception if it is being done the wrong way? (not sure if this is possible but it would really help.)
3. Has this changed in ASP.NET2?
4. LoadControl requires the path and filename of the web user control to load. Putting aside that fact that it is not strongly typed and the fact that the relative location to the web user control is a problem if the location of web pages changes, etc, etc, etc, is it possible for a web page or web user control to report their own path and file name? I have searched but did not find. If this was possible then we could go LoadControl(WebUserControl.GetAbsolutePathAndFileN ame()) which would make for much more maintanable code.
5. In response to question 4.0, has this at all changed for ASP.NET2?
Regards
Dave A
protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;
private void Page_Load(object sender, System.EventArgs e)
{
PlaceHolder1.Controls.Add(new Button());
PlaceHolder1.Controls.Add(new Label());
PlaceHolder1.Controls.Add(new DropDownList());

PlaceHolder1.Controls.Add(LoadControl("WebUserCont rol1.ascx"));
}

Dec 7 '05 #3

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

Similar topics

1
by: tascien | last post by:
I have a user control that has dynamic controls loaded to it in Page_Init. this works well. I have a function inside the user control: GetValues() When i add this usercontrol to a page:...
1
by: wASP | last post by:
Hello everyone, I'm wondering if anyone knows of any good documentation on dynamic Loading of ASP.NET User Controls using the LoadControl Method? ( ... or with any other method in general?) ...
3
by: Adie | last post by:
Hi, I wonder if anyone can tell me how to add user controls to a page dynamically? In context: I have a bunch of HTML tables which due to time constraints can't be populated to a database, so I...
4
by: Tim::.. | last post by:
Can someone please tell me why the following dynamic refresh doesn't work! Thanks Inline code... <!----- dynamically filled META REFRESH element -----> <meta id="mtaRefresh" runat="server" />...
1
by: npverni | last post by:
I have a fairly complex form that needs to load and maintain the state of several different dynamic user controls. Here is the workflow: 1) A series of editable user controls (each containing...
1
by: Diffident | last post by:
Hello All, I am trying to add dynamic controls onto my page and here is how I am doing that. I have a page which has a button called as "AddMoreControls" and in this button's event handler I...
3
by: HP | last post by:
Hi there The problem of dynamically created controls vs viewstate is widely known one. To access values of controls they have to be recreated on Page_Load. Unfortunately it causes many...
2
by: germ | last post by:
I am moving a web application from 1.1 to 2.0 This site builds pages dynamically as : PlaceHolder.Controls.Add(LoadControl("~/Controls/Ctl1.ascx")); Everything is working fine as long as the web...
4
Frinavale
by: Frinavale | last post by:
Introduction Sometimes, when developing web applications, we need to be able to dynamically load controls based on user selections. The following article describes a simple scenario where TextBox...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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
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...

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.