I just don't get dynamic loading of controls

D

Dave A

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("WebUserControl1.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.GetAbsolutePathAndFileName()) 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("WebUserControl1.ascx"));
}
 
K

Kevin Spencer

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.

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("WebUserControl1.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.GetAbsolutePathAndFileName()) 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("WebUserControl1.ascx"));
}
 
K

Karl Seguin

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!



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("WebUserControl1.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.GetAbsolutePathAndFileName()) 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("WebUserControl1.ascx"));
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top