Dynamically adding in User Controls

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tshad

    #1

    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="VB" trace="false" debug="true" ContentType="te xt/html"
    ResponseEncodin g="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="Naviga te" Src="sdhcNaviga te.ascx" %>
    <%@ Register TagPrefix="fts" TagName="Naviga te" Src="ft2Navigat e.ascx" %>
    <%@ Register TagPrefix="fts" TagName="Naviga te" 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.a scx",
    "ft2Navigate.as cx" or "sbNavigate.asc x"

    *************** *************** *************** *************** *************** ***
    <%@ Page Language="VB" trace="false" debug="true" ContentType="te xt/html"
    ResponseEncodin g="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="Naviga te" Src="" %>
    <script runat="server">
    Sub Page_Load(sende r as Object, e as EventArgs)
    if not IsPostBack

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

    end if
    </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


  • gareth

    #2
    Re: Dynamically adding in User Controls

    Hi,

    The way I do this:

    Don't put any register tag prefixes.
    Put a placeholder where you want the control to appear.
    In your code behind load the control you want, i.e.:

    Dim MyControl As New Control
    MyControl = LoadControl("Tr avelClinicSearc h.ascx")
    plcDatabases.Co ntrols.Add(MyCo ntrol)

    Where plcDatabases is the name of the placeholder. Obviously, the
    control name can be a variable.

    Hope this helps.

    Gareth

    tshad wrote:
    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="VB" trace="false" debug="true" ContentType="te xt/html"
    ResponseEncodin g="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="Naviga te" Src="sdhcNaviga te.ascx" %>
    <%@ Register TagPrefix="fts" TagName="Naviga te" Src="ft2Navigat e.ascx" %>
    <%@ Register TagPrefix="fts" TagName="Naviga te" 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.a scx",
    "ft2Navigate.as cx" or "sbNavigate.asc x"
    >
    *************** *************** *************** *************** *************** ***
    <%@ Page Language="VB" trace="false" debug="true" ContentType="te xt/html"
    ResponseEncodin g="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="Naviga te" Src="" %>
    <script runat="server">
    Sub Page_Load(sende r as Object, e as EventArgs)
    if not IsPostBack
    >
    Set the Src of the control here (or maybe in the html area)
    >
    end if
    </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

    Comment

    • tshad

      #3
      Re: Dynamically adding in User Controls

      That was what I was looking for.

      I wanted to be able to load a control depending on settings in my session
      variables and this would allow me to do that.

      Thanks,

      Tom
      "gareth" <garethricharda dams@gmail.comw rote in message
      news:1153990871 .597601.90920@i 3g2000cwc.googl egroups.com...
      Hi,
      >
      The way I do this:
      >
      Don't put any register tag prefixes.
      Put a placeholder where you want the control to appear.
      In your code behind load the control you want, i.e.:
      >
      Dim MyControl As New Control
      MyControl = LoadControl("Tr avelClinicSearc h.ascx")
      plcDatabases.Co ntrols.Add(MyCo ntrol)
      >
      Where plcDatabases is the name of the placeholder. Obviously, the
      control name can be a variable.
      >
      Hope this helps.
      >
      Gareth
      >
      tshad wrote:
      >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="VB" trace="false" debug="true" ContentType="te xt/html"
      >ResponseEncodi ng="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="Naviga te" Src="sdhcNaviga te.ascx"
      >%>
      ><%@ Register TagPrefix="fts" TagName="Naviga te" Src="ft2Navigat e.ascx" %>
      ><%@ Register TagPrefix="fts" TagName="Naviga te" 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.a scx" or "sbNavigate.asc x"
      >>
      >************** *************** *************** *************** *************** ****
      ><%@ Page Language="VB" trace="false" debug="true" ContentType="te xt/html"
      >ResponseEncodi ng="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="Naviga te" Src="" %>
      ><script runat="server">
      > Sub Page_Load(sende r as Object, e as EventArgs)
      > if not IsPostBack
      >>
      > Set the Src of the control here (or maybe in the html area)
      >>
      > end if
      ></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
      >

      Comment

      Working...