Re: Dynamically adding in User Controls



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.com> wrote in message
news:eRHcB6TsGHA.3480@xxxxxxxxxxxxxxxxxxxxxxx
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" <tscheiderich@xxxxxxxxxxxxxxx> wrote in message
news:e3x7oCQsGHA.4596@xxxxxxxxxxxxxxxxxxxxxxx
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





.



Relevant Pages

  • Re: Dynamically Loading a UserControl
    ... only one user control is being rendered. ... SelectedIndexChanged event do a Server.Transfer and apend a QueryString to ... Requestquery string to know which control to load. ... > keeping track of what UserControl to load upon a page being posted back. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Dynamically Loading a UserControl
    ... > only load the control named in the ViewState if the PostBack was NOT ... >> keeping track of what UserControl to load upon a page being posted back. ...
    (microsoft.public.dotnet.framework.aspnet)
  • RE: Question Title: Unable to Load a .NET UserControl In Web Browser
    ... and add a Button control on it. ... the control does NOT LOAD. ... > AssemblyKeyFile() location. ... > control and SHDocVW to host Office documents in a .NET Assembly UserControl ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: Ajax TabContainer behaviour like Windows TabControl?
    ... Database while Page load. ... There is a TabContainer control below the Menu Control. ... Now i want to add corresponding UserControl dynamically when user clicks on ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Required multiple instances of a UserControl loaded at runtime
    ... the user control when a button onclick event fires and also in a Placeholder ... Load event in the right conditions. ... some certain place so that the usercontrol can access it later. ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)