473,770 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Co ntrols.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.Co ntrols.Add(Load Control("WebUse rControl1.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 "LoadContro l" 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(Web UserControl.Get AbsolutePathAnd FileName()) 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.W ebControls.Plac eHolder PlaceHolder1;
private void Page_Load(objec t sender, System.EventArg s e)
{
PlaceHolder1.Co ntrols.Add(new Button());
PlaceHolder1.Co ntrols.Add(new Label());
PlaceHolder1.Co ntrols.Add(new DropDownList()) ;

PlaceHolder1.Co ntrols.Add(Load Control("WebUse rControl1.ascx" ));
}

Dec 7 '05 #1
2 1799
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**@sigmasolu tionsdonotspamm e.com.au> wrote in message
news:ep******** *******@tk2msft ngp13.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.Co ntrols.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.Co ntrols.Add(Load Control("WebUse rControl1.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
"LoadContro l" 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(Web UserControl.Get AbsolutePathAnd FileName()) 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.W ebControls.Plac eHolder PlaceHolder1;
private void Page_Load(objec t sender, System.EventArg s e)
{
PlaceHolder1.Co ntrols.Add(new Button());
PlaceHolder1.Co ntrols.Add(new Label());
PlaceHolder1.Co ntrols.Add(new DropDownList()) ;

PlaceHolder1.Co ntrols.Add(Load Control("WebUse rControl1.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**@sigmasolu tionsdonotspamm e.com.au> wrote in message news:ep******** *******@tk2msft ngp13.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.Co ntrols.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.Co ntrols.Add(Load Control("WebUse rControl1.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 "LoadContro l" 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(Web UserControl.Get AbsolutePathAnd FileName()) 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.W ebControls.Plac eHolder PlaceHolder1;
private void Page_Load(objec t sender, System.EventArg s e)
{
PlaceHolder1.Co ntrols.Add(new Button());
PlaceHolder1.Co ntrols.Add(new Label());
PlaceHolder1.Co ntrols.Add(new DropDownList()) ;

PlaceHolder1.Co ntrols.Add(Load Control("WebUse rControl1.ascx" ));
}

Dec 7 '05 #3

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

Similar topics

1
1288
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: test.aspx and i call GetValues(), it returns me the values of the dynamically added controls correctly.
1
1393
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?) In particular, I'm looking for some documentation on the functions for creating and manipulating those controls programmatically - besides and much like the ones I'm using now:
3
1215
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 was planning on cutting the HTML source and creating a user control from said source. I will then populate the relevant database for the product with the name of the user control, so if someone selects product "blah" the query returns the name of...
4
3086
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" /> ...CodeBehind
1
1508
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 basic web form elements: textboxs, buttons, etc) are loaded from an xml file. 2) The user can either edit these user controls or select from a list of user controls to add to teh page. 3) The user clicks "save" to save the content of all of...
1
2204
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 am creating controls dynamically and adding them to a panel on the page. For example, if the button is clicked once, the page is posted back and the controls are added properly. However, if I click the "AddMoreControls" for the second time the...
3
2587
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 problems in the following (rather common, I guess) scenario: The controls are created dynamically. Their number and contents
2
1942
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 site is updateable - the .ascx files exist on disk. I would like to use the non-updateable and single assembly options - no ..ascx files. I thought that maybe this would work :...
4
51672
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 controls need to be dynamically loaded according to user input. This simple example can be further extended to dynamically load custom web user controls. Background Please familiarize yourself with the ASP.NET Page Life Cycle. It is crucial to...
0
9617
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10099
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10037
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7456
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
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 we have to send another system
2
3609
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.