By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,027 Members | 841 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,027 IT Pros & Developers. It's quick & easy.

Placeholder and UserControl

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Ben de Vette" <Be****************@profiler.nl> wrote in message
news:10*************@corp.supernews.com...
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?
--
John Saunders
johnwsaundersiii at hotmail
Nov 18 '05 #2

P: n/a
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


"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:eM**************@TK2MSFTNGP11.phx.gbl...
"Ben de Vette" <Be****************@profiler.nl> wrote in message
news:10*************@corp.supernews.com...
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?
--
John Saunders
johnwsaundersiii at hotmail

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.