dynamically including user control ...

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.
LVL 1
vbnewbie01Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
mdamicoConnect With a Mentor Commented:
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
 
KCDeveloperCommented:

hello..

if for example UserContorl1.ascx

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

bye,
KC
0
 
vbnewbie01Author Commented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
KCDeveloperCommented:

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
 
KCDeveloperConnect With a Mentor Commented:

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
 
vbnewbie01Author Commented:
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
 
KCDeveloperCommented:

ok..

what do the 30 page contain?

kC
0
 
vbnewbie01Author Commented:
very simple html.  

just a table that holds the photo thumbnails.
0
 
mdamicoCommented:
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
 
vbnewbie01Author Commented:
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
 
vbnewbie01Author Commented:
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
 
vbnewbie01Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.