Home All Groups Group Topic Archive Search About
Author
5 Jan 2006 7:49 AM
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

Author
5 Jan 2006 6:26 PM
Demetri
Page.Controls.Add() is your friend.
--
-Demetri


Show quoteHide quote
"Allan Ebdrup" wrote:

> 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
>
>
>
Author
11 Jan 2006 9:37 PM
Keith Patrick
As is Control.LoadControl(String)
Author
12 Jan 2006 12:54 PM
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
Author
12 Jan 2006 1:11 PM
Allan Ebdrup
Found the problem, the control was nested inside the form
Author
12 Jan 2006 1:29 PM
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
Author
12 Jan 2006 1:36 PM
Allan Ebdrup
Tried Page_Init, still can't modify Page.Controls
Author
12 Jan 2006 2:11 PM
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
Author
12 Jan 2006 3:48 PM
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.