title: ASP.NET Dynamically Change User Control Source
body: <h3>The Scenario</h3>
  
  <p>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.</p>
  
  <h3>The current code to register the user control</h3>
  
  <pre><code>&lt;%@ Register TagPrefix="chase" TagName="topMenu"  Src="~/UserControls/TopMenu.ascx" %&gt;
  </code></pre>
  
  <h3>Inside the body tags</h3>
  
  <pre><code> &lt;div id="menuRow"&gt;
       &lt;chase:topMenu runat="server" /&gt;
   &lt;/div&gt;
  </code></pre>
  
  <h3>The Question</h3>
  
  <p>Is there anyway I can change the "SRC" attribute in the register code dynamically to use a different user control?!</p>
  
  <p>Help greatly appreciated</p>
  
  <h3>EDIT:</h3>
  
  <p>Tried This Code But I Receive An 'Invalid Cast Exception'</p>
  
  <pre><code>TopMenu uh3 = (TopMenu)this.LoadControl("~/UserControls/TopMenu.ascx");
              menuRow.Controls.Add(uh3);
  </code></pre>
  
  <p>'Unable to cast object of type 'ASP.usercontrols_topmenu_ascx' to type 'SwintonTaxiWeb.UserControls.TopMenu'.'</p>