Solved

dynamically including user control ...

Posted on 2004-09-16
12
222 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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

20 Experts available now in Live!

Get 1:1 Help Now