Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

I just don't get dynamic loading of controls

84 views
Skip to first unread message

Dave A

unread,
Dec 6, 2005, 9:21:16 PM12/6/05
to
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"));
}

Kevin Spencer

unread,
Dec 6, 2005, 9:32:48 PM12/6/05
to
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$5gTt%23FH...@tk2msftngp13.phx.gbl...

REMOVE @removeopenmymindremovemetoo.andmenet Karl Seguin

unread,
Dec 6, 2005, 9:46:50 PM12/6/05
to
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
 

 
0 new messages