How to Load User Control From Within User Control

I'd like to load a user control, that has its code-behind compiled into an assembly, from within another user control.  The user control you see below is used as a page template, contained within a .aspx file.  There is no code in the MySite.Web.UI.Pages.PageTemplate class.


<%@ Control Language="C#" Inherits="MySite.Web.UI.Pages.PageTemplate" %>
<%@ Register TagPrefix="mysite" Namespace="MySite.Web.UI.Controls" Assembly="MySite.Web" %>
<div id="container">
<div id="pageHeader">
<mysite:Header id="Header" runat="server" />
</div>
<div id="linkList">
<mysite:Navigation id="Navigation" runat="server" />
</div>
<div id="pageBody">
<asp:PlaceHolder id="ContentSkin" runat="server" />
</div>
<div id="pageFooter">
<mysite:Footer id="Footer" runat="server" />
</div>
</div>

None of the user controls are rendered on the page. Here's the Header user control:

<%@ Control Language="c#" AutoEventWireup="true" Inherits="MySite.Web.UI.Controls.Header" %>
<h1>My Site</h1>

And here's the class that the user control inherits:

public class Header : System.Web.UI.UserControl
{
string _skinFileName = "Header.ascx";
}

I understand that I need to do more than just assign a file name inside the class, but I'm not sure exactly what needs to be done to get the code-behind and HTML to come together and get rendered in the user control listed above. I've messed some with overriding the OnInit event but haven't had much luck. Can someone please point me in the right direction?
The Commander of the Faithful
0
writhe
2/11/2004 5:29:20 AM
asp.net.web-forms 93655 articles. 6 followers. Follow

4 Replies
1126 Views

Similar Articles

[PageSpeed] 24
Get it on Google Play
Get it on Apple App Store

You dont have a Tagname attribute in your Register directive:

<%@ Register TagPrefix="mysite" Namespace="MySite.Web.UI.Controls" Assembly="MySite.Web" %>
should be:
<%@ Register TagPrefix="mysite" Namespace="MySite.Web.UI.Controls" Assembly="MySite.Web" TagName="header" %>

if that doesn't work, try loading it through a path:
<%@ Register TagPrefix="mysite" TagName="header" Src="includes/header.ascx" %>
0
javan15
2/11/2004 8:40:38 AM
That is true.  However, I'm trying to implement the user controls the same way that the Community Starter Kit/GotCommunityNet does.  They don't use the Tagname or Src attributes; instead, they override the CreateChildControls() method which I have had no luck doing.  Here is an abbreviated example of their page template user control:


<%@ Control Language="C#" %>
<%@ Register TagPrefix="community" Namespace="ASPNET.StarterKit.Communities" Assembly="ASPNET.StarterKit.Communities" %>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<!-- Outer Left Line -->
<td bgcolor="#DCDDDE" width="2"><img src='<%=ResolveUrl("../../Images/Spacer.gif")%>' width="2" height="1" border="0"></td>
<!-- Inner Table -->
<td valign="top">
<table width="100%" cellpadding="0" cellspacing="0" border="0" height="70">
<!-- Logo Row -->
<tr>
<td bgcolor="#2f7eb0" width="150" height="70" align="center" valign="middle" cellpadding="0" cellspacing="0" border="0" class="LogoCell">
<table width="150" height="70">
<tr>
<td align="center" valign="middle" class="LogoCell">
<community:Logo id="logo" Runat="Server"/>
</td>
</tr>
</table>
</td>
....

There's got to be a way to load the HTML skin file in the codebehind, I just don't know exactly how to do it.
The Commander of the Faithful
0
writhe
2/11/2004 2:24:58 PM
Can I see your Codebehind code that is trying to load this?
0
javan15
2/12/2004 1:49:30 PM
I found an article that explains what I'm trying to do and I still can't get my code to work.  I have created a class called SkinnedControl that my custom control inherits from.


SkinnedControl.cs
using System;
using System.Web.UI;
namespace MySite.Framework
{
public abstract class SkinnedControl : Control, INamingContainer
{
private Control _skinControl = null;
private string _skinFileName = null;
public string SkinFileName
{
get { return _skinFileName; }
set { _skinFileName = value; }
}
protected override void CreateChildControls()
{
Controls.Clear();
base.CreateChildControls();
try
{
// Try to load the skin
PreInitialize();
// Add the skin to the ControlCollection
Controls.Add( _skinControl );
// Initialize the skin
Initialize( _skinControl );
}
catch ( System.Exception e ) {
//throw new Exception( e.Message );
}
}
private void PreInitialize()
{
_skinControl = Page.LoadControl( _skinFileName );
}
protected abstract void Initialize( Control _skinControl );
}
}

And here is the code behind for the custom control:

Header.cs
namespace MySite.Web.UI.Controls
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for Header.
/// </summary>
public class Header : MySite.Framework.SkinnedControl
{
string _skinFileName = "/Skins/Default/Controls/Header.ascx";
public Header()
{
if ( SkinFileName == null )
SkinFileName = BuildSkinKey();
}
protected override void Initialize(System.Web.UI.Control _skinControl)
{

}
private string BuildSkinKey()
{
// Do we have a skin file?
if ( _skinFileName == null )
throw new Exception("The SkinFileName property of the control must be set.");
return ("/Skins/Default/Controls/" + _skinFileName);
}
}
}

The Commander of the Faithful
0
writhe
2/15/2004 5:35:04 AM
Reply:

Similar Artilces:

UpdatePanel within a web user control resets data of controls outside the user control
Hello, I have a usercontrol on my page which displays a running marquee text. It makes use of an UpdatePanel (within the user control) to refresh the marquee text (within the updatepanel) at periodic intervals. For this I added a timer control and its tick event as an async postback trigger for the UpdatePanel. I have a lot of textboxes and dropdown list boxes (all with runat=server) on the page outside the above user control. These are also contained in different UpdatePanels. Problem is, when the user control does a postback to refresh the marquee text, it causes some of...

User Control Within UserControl
If I have a user control A and then inside it a user control B embedded/referenced within it.  User Control A has a panel defined (cpeNewUser) that I want to manipulate from the code behind of User Control B based on some event that happens.So putting this in my code behind of User Control B won't work as it has no idea of cpeNewUser:cpeNewUser.Collapsed = true;So is it because only at runtime they are married up and because of this you cannot access each other's objects at compile time?  How would I get this to work becuase I'm using all user controls inside a AJAX Too...

Web User Control to Web User Control...
Hi, I have 2 User controls on a page and want Control1 to call a method of Control2. Is this Possible? Example: Con1 = Datalist showing products, each with a Quantity Box and a Button. Con2 = Repeater showing Cart Preview (name, Price) in page.aspx. When I click on the button on the DataList in Con1 it adds the item into the Session based Cart. However it does not update the Cart Preview. I need to call BindPreview() in Con2 in order to update the Cart Preview... Does that make sense? argh! any help much appreciated! PeteWeb Design Nottingham, UK Well I can call BindPreview() from m...

How to load user control within another user control?
I have many user controls. One of them is Banner.ascx which I may load it within anothers. I tried to load Banner.ascx within Footer.ascx but  I got an error "The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases."Any ideas?Any answer is appreciated highly.Thanks. Hi friend, Check the following paragraph, perhaps it can help you,   Does your application run on .NET Framework 2.0 or above? If so, from the error message, the cause of the error is that you are trying to access a property on a child control in OnInit phase, wh...

access controls on web user control x from web user control Y
I have 2 web users controls in a page and I would like to get or set the selected values of a few dropdown boxes on web user control X from web user control Y and I also would like to know how to access functions that are on a web user control X from a web user control Y, is this possible? How can I do it? User can access and set the values of the User Control from .aspx page through properties,using javascript and in code-behind of aspx page.The details of it are shown below Using PropertiesIf the Sample.ascx control has two textboxes and submit button.You can access the valu...

dynamically load user controls within a user control problem
I am dynamically loading a set of user controls from within a user control (static user control). I add my user controls, at runtime, to the controls collection of a Panel control that is on my static user control. I reload my dynamic user controls each time my static user control runs its Page_Load event. Everything works fine, but my dynamic user control button events do not fire. Any help appreciated.   Regards, Paul. Hi,  how and where do you add the event handler to your user control button? I have a page (aspx) that has a user control (static) on it...

Javascript inside "web user control" not run if load "web user control" under ajax UpdatePanel
I have this problem and please if some can help me, I place here a sample code to see, but in my real problem the web user control have a very complicate javascript. I have a web user control that have some javascript inside, let say for example.1 <script type="text/javascript" > 2 function RunMe() 3 { 4 alert("Client id:<%=ClientID%>"); 5 } 6 </script> 7 8 9 <a href="javascript:RunMe()">Run javascript</a> a simple call to a simple javascript, inside the web user control And now I have...

Problem in accessing web form data in user controls when create user control dynamically
HI. I have a problem using web user control. I designed a user control named NewsForm. This user control contains some web form elements like <asp:textbox>. When I add it in an .ASPX page, all things may be true. I can know which user filles in textbox (for example and other controls) <%@ Register TagPrefix="uc1" TagName="NewsForm" Src="NewsForm.ascx" %> and <UC1:NewsForm id="newsForm" runat="server"></UC1:NewsForm> But when i create it in code behined dynamically, however user control has web form controls but their value are blank for example //aspx co...

How do you expose an event from a server control within an user control on an web form?
I have a calendar inside of an user control Simple.ascx The user control is sitting on my web form Simple.aspx. How do I access the VisibleMonthChanged event from my web form Simple.aspx? Is it possible? Yes Create Event in usercontrol which fires on page when VisibleMonthChanged event fires on usercontrol. Jigar Desai-----------------------Do not forget to "Mark as Answer" on the post that helped you. Ok, I created the event Public Event MonthChanged() in Simple.ascx.vb. Then I add the follow to Simple.aspx.vb Private Sub Simple_MonthChanged() Si...

Accesing User Controls within User Control
I've a web page which is using a user control.  I need to put several user controls on that user control (which I'm loading on web page.).  My problem is how to access the user controls property on (which are on the user control) from web page. Please help. Thanks in advance. you need to create other property in your web control which will return an instance of your 2nd web controlDomain Names | ASP.NET 3.5 Hosting - $3.99/mo (10 GB Space, 300 GB Transfer, 200 MB MS SQL 2000/2005) Are you saying that you have multiple controls on one page that you want to talk to eachot...

Accessing a user control within a user control
Hi all, I have a user control and I'm using another user control within it but I'm unable to access the members of this nested user control. Can someone please tell me how to do this? Thanks   Exposes the needed members as properties and access them.  Hosam Kamel Remember to click on Mark as answer on the post that helped you  make the nested user control a property of the other user control.Regards Please click "Mark as Answer" if my reply solved your problem.Visit My Blog Can someone please provide an example? Thanks  For example if...

User Controls Defined within User Controls
I have an ASP.NET web app that contains two primary user controls (call them control A and control B). In my app I have I also have another user control (control C). Both control A and control B define an instance of control C.  in my default.aspx page I defined them in the following order: Control B Control A Only one control is visible at a time depending on a previous user selection. I find that if the user selected control A, then somehow the contol C defined in control A, gets criss-crossed with the control C defined in control B.  The vice versa will occur of I...

User Controls in User Controls?
I have the follow set up and it is throwing an error "Unhandled error loading module" ... is there some trick to this that I don't know?I have a project A that uses controls from project B (project B is just a project with all my user controls used through out other modules).  project A uses user control 1 from project B with out any issues at all - great.  But then I wanted to add user control 2 to user control 1 and that is when I get my error.  In fact I don't even have the user control 2 in user control 1, just when I put the register part into user control 1 is when it al...

User Control in a User Control
I have a first user control that contain a second user control. If I cahe the first user control can I still dynamically control the second one or it is cached with the first one therefore can not control it anymore? Thanks When your first control is cached, its rendered out and its output is stored in the cache. On every request to the page containing your control that can satisfy it from cache, the output will just simply be inserted - your first control will never be instantiated or run in that case, so the second one wont be either. Hope this helps,Mike VolodarskyProgram Manager...

Web resources about - How to Load User Control From Within User Control - asp.net.web-forms

Control - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

Firefighters battle out of control bushfire in Bullsbrook
Around 70 firefighters are battling an out of control bush-fire in Bullsbrook.

Firefighters battle out of control bushfire in Bullsbrook
Around 70 firefighters are battling an out of control bush-fire in Bullsbrook.

Firefighters battle out of control bushfire in Bullsbrook
Around 70 firefighters are battling an out of control bush-fire in Bullsbrook.

Firefighters battle out of control bushfire in Bullsbrook
Around 70 firefighters are battling an out of control bush-fire in Bullsbrook.

As it happened: Fire crews battle out-of-control blazes in southern states
Emergency warnings are in place for a fire burning in Victoria's north-east while fire crews also tackle blazes in ACT, NSW and Tasmania.

Dell Issues Patch For Content Adaptive Brightness Control On The XPS 13
... issues, as all devices do. One that was very frustrating to deal with during the review was the aggressive Content Adaptive Brightness Control ...

Taliban forces take control of key district in Afghanistan's Helmand province
CNN Taliban forces take control of key district in Afghanistan's Helmand province CNN Kabul, Afghanistan (CNN) Taliban forces have taken almost ...

San Bernardino Gun Control Poll
Would Tougher Gun Control Laws Prevent Mass Shootings In California? [question q=”1″] [email][submit] *Completing this poll entitles you to Daily ...

How Sepp Blatter Lost Control Of His Monster
If Sepp Blatter could take a step back and look at the bigger picture, forgetting for a moment that today’s eight-year ban from all soccer activities ...

Resources last updated: 12/22/2015 2:14:25 PM