Solved

dynamically including user control ...

Posted on 2004-09-16
12
221 Views
Last Modified: 2010-08-05
hi there;

there is probably a simple solution to this.

using asp.net (vb), i'm including a user control in one of my aspx pages.  the thing is, the control i use will be determined by what the user chooses from a drop down menu.

my question is: how would i dynamically include that user control on my page?

for example, how would i dynamically change the src reference of the following namespace?

<%@ Register TagPrefix="UserControl" TagName="photos" Src="userControls/something.ascx" %>

thanks all.
0
Comment
Question by:vbnewbie01
  • 6
  • 4
  • 2
12 Comments
 

Expert Comment

by:KCDeveloper
ID: 12077011

hello..

if for example UserContorl1.ascx

then do Page.LoadControl(""UserControl1.ascx)

bye,
KC
0
 
LVL 1

Author Comment

by:vbnewbie01
ID: 12077706
hi kc ... thanks for the response.

can u please be a little more specific?

I have a few user controls being used on this page (all work fine).

in my code, i have to be able to match the dynamic control to the correct 'holder' within my html:


<UserControl:photoContent runat="server" id="photoContent"/>


again, here is my default namespace when the page loads:

<%@ Register TagPrefix="UserControl" TagName="photoContent" Src="userControls/default.ascx" %>


this needs to be overidden when the user selects from a drop down.

how would i code this?

thanks again.


0
 

Expert Comment

by:KCDeveloper
ID: 12078104

hai..

its been a long time since i have worked on asp.net

but i am confident that this would work

step1: inclue all the register directives for  all the user controls you want to add on the web form

example: for UserControl1.ascx, UserControl2.ascx and UserControl3.ascx

add <%@ Register TagPrefix="UserControl" TagName="photoContent" Src="userControls/UserControl1.ascx" %>
       <%@ Register TagPrefix="UserControl" TagName="photoContent" Src="userControls/UserControl1.ascx" %>
       <%@ Register TagPrefix="UserControl" TagName="photoContent" Src="userControls/UserControl1.ascx" %>

step2: now you can dynamically load any of these user controls. the rule is ..include a register directive(i mean ...don't include the register directive at runtime...you should include these register directives at design time) before dynamically loading a user control onto a web page.

hope this helps
bye,
KC
0
 

Assisted Solution

by:KCDeveloper
KCDeveloper earned 100 total points
ID: 12078160

hai...

i forgot to give a example..

<%@ Reference Control = "UserControl1.ascx" %>
<%@ Reference Control = "UserControl2.ascx" %>
<%@ Reference Control = "UserControl3.ascx" %>

and then in code you do like this

UserControl1 uc1 = (UserControl1) Page.LoadControl("UserControl1.ascx");
PlaceHolder1.Controls.Add(uc1);

0
 
LVL 1

Author Comment

by:vbnewbie01
ID: 12078804
see, the issue is, i'd really rather not have to register every single user control that i have since there will be in the neighborhood of about 100 and climbing.

basically, i have an photos.aspx page.

on this page i have a header.ascx, a photoContent.ascx (this is the one i'm inquiring about) and a footer.ascx.

the header.ascx and the footer.ascx never change.

the photoContent.ascx will however change regularly.

basically, each time i add a new page of photo's, i will be creating a new user control.  i currently have about 30 pages (and, as i mentioned, the list is growing).

when the user chooses which page they'd like to view (from a drop down menu), their selection will replace the photoContent.ascx userControl when the page is posted back.

is there not a way to put the name of the new ascx usercontrl into a session variable, then load that into the src portion of the namespace when the page is posted back?

thanks.

0
 

Expert Comment

by:KCDeveloper
ID: 12078921

ok..

what do the 30 page contain?

kC
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:vbnewbie01
ID: 12079007
very simple html.  

just a table that holds the photo thumbnails.
0
 
LVL 7

Expert Comment

by:mdamico
ID: 12079660
You don't need to register user controls that you load dynamically.


For VB Just do this in codebehind:

Dim uc1 as UserControl = Page.LoadControl("UserControl1.ascx")
Placeholder1.Controls.Add(uc1)

The only thing you would need to change is the path to the control to load different controls.

Simple example(It would be a little more complex if you also wanted to set properties for the control):

Dim controlpath as String
Select Case ControltoLoad
  Case 1
    controlpath = "UserControl1.ascx"
  Case 2
    controlpath = "UserControl2.ascx"
End Select

Dim uc1 as UserControl = Page.LoadControl(controlpath)
Placeholder1.Controls.Add(uc1)
0
 
LVL 1

Author Comment

by:vbnewbie01
ID: 12080298
mdamico; i like where you're going with this ...

here's a question ... i'm not using code behind so how would i do this inline?

please be as specific as possible as this is a concept i'm new to and not really familiar with.

thanks for that.
0
 
LVL 1

Author Comment

by:vbnewbie01
ID: 12080443
all i really need figured out is how to dynamically change (on postback) the following src in the namespace to reflect what user control to point to:

<%@ Register TagPrefix="UserControl" TagName="photoContent" Src="userControls/photoCategory.ascx" %>
0
 
LVL 7

Accepted Solution

by:
mdamico earned 200 total points
ID: 12080916
You can do a the same with "inline" code.   Just add a server script block to your page(if you don't already have one)


<script runat="server">
Sub Page_Load(sender as Object, e as System.EventArgs)

  Dim controlpath as String
  Select Case ControltoLoad
    Case 1
      controlpath = "UserControl1.ascx"
    Case 2
      controlpath = "UserControl2.ascx"
  End Select

End Sub
</script>
0
 
LVL 1

Author Comment

by:vbnewbie01
ID: 12082213
i'm increasing the points to 300 from 200 ... the original 200 goes to mdamico for his method and explanation ... 100 for KCDeveloper.

KCDeveloper ... in retrospect, u were on the right track, but i needed it explained further.  mdamico's method is exactly what i needed.

thanks to both, and to all, who replied.

Below is my final code:

Sub Page_Load(ByVal sender as system.object, ByVal e as system.EventArgs) Handles MyBase.Load
      
dim ucPhotoContent as userControl
      
if session("whatever") = "east" then
     ucPhotoContent = Page.LoadControl("userControls/photoContent/east/2004_jan10.ascx")
elseif session("whatever") = "west" then
ucPhotoContent = Page.LoadControl("userControls/photoContent/west/2004_jan09.ascx")
end if

phPhotoContent.Controls.Add(ucPhotoContent)

end sub


then in the html part of my application:

<asp:PlaceHolder runat="server" id="phPhotoContent" />

and that's it.

i found another great resource that explains this even further here:

http://aspnet.4guysfromrolla.com/articles/081402-1.aspx

again, thanks too each who contributed.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now