Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Dynamically adding in User Controls

Reply
Thread Tools

Dynamically adding in User Controls

 
 
tshad
Guest
Posts: n/a
 
      07-26-2006
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


 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      07-27-2006
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
 
 
 
tshad
Guest
Posts: n/a
 
      07-27-2006
"Teemu Keiski" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> 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
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tough Q on Dynamically adding cached User Controls Justin Dutoit ASP .Net 0 03-19-2008 12:46 AM
Dynamically adding user controls, and then setting custom properties on those instances? ASP .Net 6 12-23-2006 09:12 AM
Dynamically adding Web User Controls to ASP page Jason ASP .Net 7 05-04-2006 07:27 PM
Efficiency of dynamically adding web user controls MC D ASP .Net 4 11-18-2003 07:27 PM
dynamically adding user controls Dune ASP .Net Building Controls 6 10-03-2003 03:53 AM



Advertisments