views:

30

answers:

2

Hi all,

In Asp.Net is it possible to dynamically switch which user control gets loaded with the .aspx page.

Depending on the type of news story I would like to switch which control gets loaded.

Thanks melt

+3  A: 

Put a place holder in your page and in your code-behind file load the control on a if/then/else or switch/case logic method. That's the easiest way I see the implementation.

Erick
+1  A: 

Use LoadControl(), which is an instance method on the Page class. Then simply add it to a container's Controls collection.

             if (mytype=="news")  
             {  
                 //load the required usercontol  
                 ph.Controls.Add(LoadControl("~/usercontrols/news.ascx"));
             }  
             else  
             {  
                 ph.Controls.Add(LoadControl("~/usercontrols/somethingelse.ascx"));
             }

With "ph" being an asp:PlaceHolder control.

Wim Hollebrandse
Thanks a lot for that working solution, much appreciatedMelt
Melt