473,383 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Dynamically adding in User Controls

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
Jul 26 '06 #1
2 1811
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" <ts**********@ftsolutions.comwrote in message news:e3**************@TK2MSFTNGP04.phx.gbl...
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

Jul 27 '06 #2
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" <dave@jwa[remove.this]online.comwrote in message
news:eR**************@TK2MSFTNGP04.phx.gbl...
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" <ts**********@ftsolutions.comwrote in message
news:e3**************@TK2MSFTNGP04.phx.gbl...
>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


Jul 28 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: sameer mowade via .NET 247 | last post by:
Hello All, I have problem while dynamically removing row from the Datagrid which i have added dynamically as shown in the following code snippet. The problem is that while removing dynamically...
4
by: Bas Groeneveld | last post by:
I am developing an ASP.NET application part of which consists of a data entry wizard defined by entries in a data table - ie the controls on each page of the wizard are determined by definitions in...
6
by: Steve Booth | last post by:
I have a web form with a button and a placeholder, the button adds a user control to the placeholder (and removes any existing controls). The user control contains a single button. I have done all...
9
by: netasp | last post by:
hi all, how can I populate one aspx form when page is loading based on page ID? for example: loading page A (to search for VB code) would display labels and texboxes, dropdown lists all related...
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
9
by: Chris | last post by:
I am dynamically adding a user control to each row in a gridview. The reason I am doing it dynamically is the user control is different depending on certain data in the gridview. The gridview...
1
by: jelle.huygen | last post by:
Hello, I have a problem in ASP.NET 2.0 with the viewstate of my dynamically added user control. I have reproduced the problem with a very simple user control and a very simple page. On my...
1
by: Shraddha | last post by:
Hi, I am adding some ASP.Net user controls (.ascx file) dynamically on the button click. The user control will get added as many times userhits the button. Now on the click of the submit button, I...
4
by: Lewis Holmes | last post by:
Hi I have the following situation in one of my asp.net pages. The user can add multiple table rows to a form by selecting a button. These rows can contain asp.net controls. When this button is...
1
by: semomaniz | last post by:
I have a form where i have created the form dynamically. First i manually added a panel control to the web page. Then i added another panel dynamically and inside this panel i created tables. I have...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.