views:

575

answers:

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

A: 

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

dove
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
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
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
A: 

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
Please can you see my edit above. Whenever I try this code I get an invalid cast exception :-(
Goober
A: 

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
'DetermineTopMenu();' is a custom method that you have written isnt it?
Goober
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
A: 

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