including UserControl in UserControl

A

Alexander Widera

How can I include an usercotrol into an other usercontrol...
for example:

<UserControl:First runat="server" id="firstUC" content="..." />

and i want to refer an other UserControl (e.g. called "Second") to the
property "content" of usercontrol "First".


Thanks for help
 
K

Karl Seguin

If you want to dynamically load it, you'd do something like:

public class First : UserControl
{
private string _innerContentPath;
public string InnerContentPath{
get { return _innerCOntentPath; }
set { _innerContentPath = value;
}
Page_Load()
{
if (_innerContentPath != null)
{
Control InnerControl = Page.LoadControl(_innerContentPath);
SomePlaceHolder.Controls.Add(InnerControl);
}
}

so then you would do
<UserControl:First runat="server" id="FirstUC"
InnerContentPath="SomeOtherUserControl.ascx" />

Hope I didn't misunderstand..

Karl
 
G

Guest

You can easily nest User Controls by placing one on another. To get to
properties, however, you will have to expose teh embedded controls properties
with properties in the embedding control. Otherwise, the page does not have
direct access. Remember to add a code behind reference if you are
programatically setting values.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
K

Kevin Spencer

Just in case you did misunderstand, Karl, the other approach would be to
dynamically load the seond Control into the first via the Page:

// Assuming the UserControl FirstControl is in a tag in the Page
protected FirstUserControlType FirstControl;
private SecondUserControlType SecondControl;
public void Page_Load()
{
SecondControl = LoadControl("SecondControlType.ascx");
FirstControl.Controls.Add(SecondControl);
}

I hope at least ONE of us didn't misunderstand!

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
I'd rather be a hammer than a nail.
 
A

Alexander Widera

well,
Karl got it right :)

thanks a lot

Kevin Spencer said:
Just in case you did misunderstand, Karl, the other approach would be to
dynamically load the seond Control into the first via the Page:

// Assuming the UserControl FirstControl is in a tag in the Page
protected FirstUserControlType FirstControl;
private SecondUserControlType SecondControl;
public void Page_Load()
{
SecondControl = LoadControl("SecondControlType.ascx");
FirstControl.Controls.Add(SecondControl);
}

I hope at least ONE of us didn't misunderstand!

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
I'd rather be a hammer than a nail.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top