Placeholder and UserControl

B

Ben de Vette

Hi,

I like to load a UserControl onto a placeholder with some more control then
just doing

protected System.Web.UI.WebControls.PlaceHolder placeHolder;
UserControl ascx = (UserControl)Page.LoadControl("myUserControl.ascx");
placeHolder.Controls.Add(ascx);

More something like

UserControl uc = new myUserContol();
uc.Name = "just A Test";
uc.Initialize();
placeHolder.Contols.Add(uc);

The problem lies in the fact that any contols placed in myUserControl are
not instanciated in the second example. Any idea how I get this working?

Thanks,
Ben
 
J

John Saunders

Ben de Vette said:
Hi,

I like to load a UserControl onto a placeholder with some more control then
just doing

protected System.Web.UI.WebControls.PlaceHolder placeHolder;
UserControl ascx = (UserControl)Page.LoadControl("myUserControl.ascx");
placeHolder.Controls.Add(ascx);

More something like

UserControl uc = new myUserContol();
uc.Name = "just A Test";
uc.Initialize();
placeHolder.Contols.Add(uc);

The problem lies in the fact that any contols placed in myUserControl are
not instanciated in the second example. Any idea how I get this working?

When you use LoadControl, the HTML in the .ascx file are compiled. They are
compiled into a class which inherits from the myUserControl class. The .ascx
file isn't touched in your second case.

Is there a reason why you can't use the first way?
 
B

Ben de Vette

Thanks,

The reason I want to use the second example is that myUserControl should
load different values, depending on an external source identified by a
string.

So, I want to use the generic myUserControl to be reused over and over
again.

I already got it working with LoadControl().

What I do is:
In the containing Page (Test.aspx)
Create the UserControl with LoadControl
Then I put the newly loaded control into an Hash (control is the key),
an identifying string the value
Then I add the control to a PlaceHolder.

In this page I write a methode which will return the string when the
control is given as argument

In myUserControl
In PageLoad, I try to find the containing Page (test.aspx)
Cast the found page to Test
Call a methode on the Test instance with 'this' as argument
With the retrieved string, I can get a specific field/table from e.g. a
database

But I rather have it working without the callback from the ChildControl.

Ben
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top