Dynamically adding in User Controls

Discussion in 'ASP .Net' started by tshad, Jul 26, 2006.

  1. tshad

    tshad Guest

    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="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="VB" 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">
    Sub Page_Load(sender 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
     
    tshad, Jul 26, 2006
    #1
    1. Advertising

  2. tshad

    Teemu Keiski Guest

    Hi,

    Page.LoadControl can take name of the UC in and it returns you instance of
    the control which you shoiuld add to the Controls collection on the Page.

    Dim c As Control = Page.LoadControl("uc.ascx")
    addForm.Controls.Add(c)

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke

    "tshad" <> wrote in message
    news:%...
    > 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="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="VB" 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">
    > Sub Page_Load(sender 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
    >
     
    Teemu Keiski, Jul 27, 2006
    #2
    1. Advertising

  3. tshad

    tshad Guest

    "Teemu Keiski" <> wrote in message
    news:%...
    > Hi,
    >
    > Page.LoadControl can take name of the UC in and it returns you instance of
    > the control which you shoiuld add to the Controls collection on the Page.
    >
    > Dim c As Control = Page.LoadControl("uc.ascx")
    > addForm.Controls.Add(c)


    That was what I was looking for.

    Thanks,

    Tom
    >
    > --
    > Teemu Keiski
    > ASP.NET MVP, AspInsider
    > Finland, EU
    > http://blogs.aspadvice.com/joteke
    >
    > "tshad" <> wrote in message
    > news:%...
    >> 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="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="VB" 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">
    >> Sub Page_Load(sender 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
    >>

    >
    >
     
    tshad, Jul 27, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MC D
    Replies:
    4
    Views:
    476
    Big D
    Nov 18, 2003
  2. Jason
    Replies:
    7
    Views:
    1,233
    Rajesh CKR
    May 4, 2006
  3. Guest
    Replies:
    6
    Views:
    389
    Walter Wang [MSFT]
    Dec 23, 2006
  4. Justin Dutoit
    Replies:
    0
    Views:
    365
    Justin Dutoit
    Mar 19, 2008
  5. Dune

    dynamically adding user controls

    Dune, Sep 26, 2003, in forum: ASP .Net Building Controls
    Replies:
    6
    Views:
    189
    John Saunders
    Oct 3, 2003
Loading...

Share This Page