4

The Scenario

I have an ASP.Net Web Project that uses a master page. This master page contains a menu in the form of a user control. Sometimes I want to dynamically change this to use a different type of menu user control.

The current code to register the user control

<%@ Register TagPrefix="chase" TagName="topMenu"  Src="~/UserControls/TopMenu.ascx" %>

Inside the body tags

 <div id="menuRow">
     <chase:topMenu runat="server" />
 </div>

The Question

Is there anyway I can change the "SRC" attribute in the register code dynamically to use a different user control?!

Help greatly appreciated

EDIT:

Tried This Code But I Receive An 'Invalid Cast Exception'

TopMenu uh3 = (TopMenu)this.LoadControl("~/UserControls/TopMenu.ascx");
            menuRow.Controls.Add(uh3);

'Unable to cast object of type 'ASP.usercontrols_topmenu_ascx' to type 'SwintonTaxiWeb.UserControls.TopMenu'.'

Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
Goober
  • 13,146
  • 50
  • 126
  • 195

6 Answers6

1

What if you add your user control at runtime whichever you need.

UserControls_header3 uh3 = (UserControls_header3)this.LoadControl(header3);
phHeaderControls.Controls.Add(uh3);
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
  • Please can you see my edit above. Whenever I try this code I get an invalid cast exception :-( – Goober Oct 15 '09 at 10:58
0

Would you try having the two user controls registered on page and hide/disable accordingly.

dove
  • 20,469
  • 14
  • 82
  • 108
  • kinda tried that, and it worked for some of the other controls, but this is a web project that was built by someone else that im doing amends to, and i cant get the usual way to work.... – Goober Oct 15 '09 at 09:31
  • either way, I'm pretty sure you cannot change the register information at runtime, it would be too late in cycle. I think you need to look at a scenario where both are registered but only one used. – dove Oct 15 '09 at 09:54
  • Like i said im a big fan of the method you mentioned, thats the way I originally did it for some of the other amends, however I cannot get this same method to work for this particular control for some reason. Its been very poorly designed, messy code etc. – Goober Oct 15 '09 at 10:29
0

What's causing the dynamic changes?

You could do this in the server side code fairly easily.

In your .aspx page you could have:

<asp:Panel id="menuRow" runat="server">
</asp:Panel>

I'm using a Panel here because it gives us a nice container for controls, and outputs a <div> tag around them.

Then in your code behind, you'd have something like:

// Determine correct user control to load
string pathToUserControl = DetermineTopMenu();

// Load the user control - calling LoadControl forces the correct lifcycle events
// to fire, and ensures the control is created properly.
var topMenu = LoadControl(pathToUserControl);

// Add the control to the menuRow panels control collection.
menuRow.Controls.Add(topMenu);

I've assumed that you have some way to work out which user control you want to display, and that you've named them in such a way that generating the path to the user control is fairly trival. I've hidden that logic in the method call DetermineTopMenu because:

  1. I don't know what logic you're using
  2. It makes it easier to test, extend, etc.

For more information on LoadControl, check the documentation.

Zhaph - Ben Duguid
  • 26,785
  • 5
  • 80
  • 117
  • 'DetermineTopMenu();' is a custom method that you have written isnt it? – Goober Oct 15 '09 at 09:49
  • DetermineTopMenu would be a custom method you would have to write, yes. That's why I started by asking "What's causing the dynamic changes" - presumably you have some way of working out which user control to display, I'm just suggesting you wrap that up in a seperate method. – Zhaph - Ben Duguid Oct 15 '09 at 09:58
0

With regards to your problem about the InvalidCastException, maybe this MSDN page will help. You can try setting the class name of your control in the control's <%@ Control %> tag.

Graham Clark
  • 12,886
  • 8
  • 50
  • 82
0

I hope you will find some clues from this thread - Dynamically Change User Control in ASP.Net

Community
  • 1
  • 1
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
0

If you are using output cache, then LoadControl returns an instance of System.Web.UI.PartialCachingControl. You can then reach the cached instance through its property CachedControl, which you must cast to the appropriate UserControl-derived type.

Ricardo Peres
  • 13,724
  • 5
  • 57
  • 74