Dynamically load webcontrol

A

Allan Ebdrup

Hi
I have a scenario where I have a webcontrol on an ASP.Net 2.0 page, in some
cases I want to dynamically load another webcontrol instead of the control I
have. I want to load one of several possible webcontrols instead, and I
would like it to be so that I don't have to add a reference to all the
possible alternate webcontrols.
Is this possible?

Kind Regards,
Allan Ebdrup
 
A

Allan Ebdrup

OK
Now I've tried the following:
Create a control that's called DynamicLoad with a Label in it, and inserted
it on an ASP page Now I want to swap the DynamicLoad control with another
control, I try to find the control in Page.Controls like this:
---
foreach(Control c in Page.Controls){
Response.Write("c.GetType:" + c.GetType().ToString() + "<br>");
}
---
But the output is:
---
c.GetType:System.Web.UI.LiteralControl
c.GetType:System.Web.UI.HtmlControls.HtmlHead
c.GetType:System.Web.UI.LiteralControl
c.GetType:System.Web.UI.HtmlControls.HtmlForm
c.GetType:System.Web.UI.LiteralControl
---

So how do I find the DynamicLoad control and swap it, when Page.Controls
contains the controls inside the DynamicLoad control?

Kind Regards,
Allan Ebdrup
 
A

Allan Ebdrup

Now I've found the control I want to swap in the variable c, and I try to
execute the following:
Page.Controls[Page.Controls.IndexOf(c)] = new
System.Web.UI.HtmlControls.HtmlButton();

But I get an error that Page.Controls is read only, how do I swap the
control?
Is there some other event than Page_Load that allows you to modify
Page.Controls?

Kind Regards,
Allan Ebdrup
 
A

Allan Ebdrup

Now I've tried to override FrameworkInitialize, and the first thing I do in
my override is call base.FrameworkInitialize, but when I loop through the
controls on the page I can't find the control I want to swap, it doesn't
seem to be loaded yet.

Kind Regards,
Allan Ebdrup
 
A

Allan Ebdrup

Found the problem, seems you cant change page.controls in the control, if i
do the swap in the main pages Page_Load there is no problem.
 

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
474,264
Messages
2,571,065
Members
48,770
Latest member
ElysaD

Latest Threads

Top