including UserControl in UserControl

  • Thread starter Thread starter Alexander Widera
  • Start date Start date
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
 
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
 
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!
***************************
 
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.
 
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

Back
Top