[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

dynamically including user control ...

Posted on 2004-09-16
12
Medium Priority
?
231 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Assisted Solution

by:KCDeveloper
KCDeveloper earned 300 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 600 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

650 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