Dynamically adding in User Controls

T

tshad

Is there a way to dynamically add in User Controls where I would get the
name from a session variable:

If I have the following code:
******************************************************************************
<%@ Page Language="C#" trace="false" debug="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<%@ Register TagPrefix="fts" TagName="Navigate" Src="sdhcNavigate.ascx" %>
<%@ Register TagPrefix="fts" TagName="Navigate" Src="ft2Navigate.ascx" %>
<%@ Register TagPrefix="fts" TagName="Navigate" Src="sbNavigate.ascx" %>

<html>
<head>
<title>:: Staffing Workshop ::</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<body id="myBody" runat="server">
<form id="addForm" runat="server">

<fts:Navigate runat="Server"/>

</form>
</body>
</html>
********************************************************************************

I would like to change it so that I can do something like changing the Src
attribute of the Register statement based on a session variable.

For example if my session variable "company" was either "sdhcNavigate.ascx",
"ft2Navigate.ascx" or "sbNavigate.ascx"

******************************************************************************
<%@ Page Language="C#" trace="false" debug="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<%@ Register TagPrefix="fts" TagName="Navigate" Src="" %>
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{


Set the Src of the control here (or maybe in the html area)

}
</script
<html>
<head>
<title>:: Staffing Workshop ::</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<body id="myBody" runat="server">
<form id="addForm" runat="server">

<fts:Navigate runat="Server"/>

</form>
</body>
</html>
********************************************************************************

I would use this in all my 50 pages to load the control based on the
company.

Thanks,

Tom
 
D

Dave Sexton

Hi Tom,

You could load the appropriate UserControl into a PlaceHolder in a Page.Init event handler:

<!-- declare the placeholder -->
<asp:placeHolder runat="server" id="headerPlaceHolder" />

<script runat="server">
void Page_Init(object sender, EventArgs e)
{
// Even on post backs the following code must be executed to ensure that the UserControl is part of the
// page hierarchy of controls

// create the virtual path to the UserControl using data from the current Session
string headerControlPath = "~/SegmentsByCompany/" + Session["CompanyName"] + "_Header.ascx";

// load the appropriate UserControl
Control headerControl = Page.LoadControl(headerControlPath);

// add the UserControl to the PlaceHolder
headerPlaceHolder.Controls.Add(headerControl);
}
</script>
 
T

tshad

That's what I was looking for.

This would allow me to load or not load depending on values in my session
variables.

Thanks,

Tom
Dave Sexton said:
Hi Tom,

You could load the appropriate UserControl into a PlaceHolder in a
Page.Init event handler:

<!-- declare the placeholder -->
<asp:placeHolder runat="server" id="headerPlaceHolder" />

<script runat="server">
void Page_Init(object sender, EventArgs e)
{
// Even on post backs the following code must be executed to ensure
that the UserControl is part of the
// page hierarchy of controls

// create the virtual path to the UserControl using data from the
current Session
string headerControlPath = "~/SegmentsByCompany/" +
Session["CompanyName"] + "_Header.ascx";

// load the appropriate UserControl
Control headerControl = Page.LoadControl(headerControlPath);

// add the UserControl to the PlaceHolder
headerPlaceHolder.Controls.Add(headerControl);
}
</script>

--
Dave Sexton

tshad said:
Is there a way to dynamically add in User Controls where I would get the
name from a session variable:

If I have the following code:
******************************************************************************
<%@ Page Language="C#" trace="false" debug="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<%@ Register TagPrefix="fts" TagName="Navigate" Src="sdhcNavigate.ascx"
%>
<%@ Register TagPrefix="fts" TagName="Navigate" Src="ft2Navigate.ascx" %>
<%@ Register TagPrefix="fts" TagName="Navigate" Src="sbNavigate.ascx" %>

<html>
<head>
<title>:: Staffing Workshop ::</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<body id="myBody" runat="server">
<form id="addForm" runat="server">

<fts:Navigate runat="Server"/>

</form>
</body>
</html>
********************************************************************************

I would like to change it so that I can do something like changing the
Src attribute of the Register statement based on a session variable.

For example if my session variable "company" was either
"sdhcNavigate.ascx", "ft2Navigate.ascx" or "sbNavigate.ascx"

******************************************************************************
<%@ Page Language="C#" trace="false" debug="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<%@ Register TagPrefix="fts" TagName="Navigate" Src="" %>
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{


Set the Src of the control here (or maybe in the html area)

}
</script
<html>
<head>
<title>:: Staffing Workshop ::</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<body id="myBody" runat="server">
<form id="addForm" runat="server">

<fts:Navigate runat="Server"/>

</form>
</body>
</html>
********************************************************************************

I would use this in all my 50 pages to load the control based on the
company.

Thanks,

Tom
 

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

Top