Solved

dynamically including user control ...

Posted on 2004-09-16
12
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 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