Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is, I think, one of the common pitfalls for asp.net programmers but isn't actually that hard to get it right when you know what is going on (always remember your viewstate!). </p> <p>the following piece of code explains how things can be done. It's a simple page where a user can click on a menu which will trigger an action that will add a user control to the page inside the updatepanel.<br> (This code is borrowed <a href="http://geekswithblogs.net/rashid/archive/2007/08/11/Loading-UserControl-Dynamically-in-UpdatePanel.aspx" rel="noreferrer">from here</a>, and has lots more of information concerning this topic)</p> <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="SampleMenu1.aspx.cs" Inherits="SampleMenuPage1" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" &gt; &lt;head runat="server"&gt; &lt;title&gt;Sample Menu&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;asp:Menu ID="Menu1" runat="server" OnMenuItemClick="Menu1_MenuItemClick"&gt; &lt;Items&gt; &lt;asp:MenuItem Text="File"&gt; &lt;asp:MenuItem Text="Load Control1"&gt;&lt;/asp:MenuItem&gt; &lt;asp:MenuItem Text="Load Control2"&gt;&lt;/asp:MenuItem&gt; &lt;asp:MenuItem Text="Load Control3"&gt;&lt;/asp:MenuItem&gt; &lt;/asp:MenuItem&gt; &lt;/Items&gt; &lt;/asp:Menu&gt; &lt;br /&gt; &lt;br /&gt; &lt;asp:ScriptManager ID="ScriptManager1" runat="server"&gt;&lt;/asp:ScriptManager&gt; &lt;asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"&gt; &lt;ContentTemplate&gt; &lt;asp:PlaceHolder ID="PlaceHolder1" runat="server"&gt;&lt;/asp:PlaceHolder&gt; &lt;/ContentTemplate&gt; &lt;Triggers&gt; &lt;asp:AsyncPostBackTrigger ControlID="Menu1" /&gt; &lt;/Triggers&gt; &lt;/asp:UpdatePanel&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>and </p> <pre><code>using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class PlainSampleMenuPage : System.Web.UI.Page { private const string BASE_PATH = "~/DynamicControlLoading/"; private string LastLoadedControl { get { return ViewState["LastLoaded"] as string; } set { ViewState["LastLoaded"] = value; } } private void LoadUserControl() { string controlPath = LastLoadedControl; if (!string.IsNullOrEmpty(controlPath)) { PlaceHolder1.Controls.Clear(); UserControl uc = (UserControl)LoadControl(controlPath); PlaceHolder1.Controls.Add(uc); } } protected void Page_Load(object sender, EventArgs e) { LoadUserControl(); } protected void Menu1_MenuItemClick(object sender, MenuEventArgs e) { MenuItem menu = e.Item; string controlPath = string.Empty; switch (menu.Text) { case "Load Control2": controlPath = BASE_PATH + "SampleControl2.ascx"; break; case "Load Control3": controlPath = BASE_PATH + "SampleControl3.ascx"; break; default: controlPath = BASE_PATH + "SampleControl1.ascx"; break; } LastLoadedControl = controlPath; LoadUserControl(); } } </code></pre> <p>for the code behind.</p> <p>That's basically it. You can clearly see that the viewstate is being kept with <em>LastLoadedControl</em> while the controls themselves are dynamically added to the page (inside the updatePanel (actually inside the placeHolder inside the updatePanel) when the user clicks on a menu item, which will send an asynchronous postback to the server.</p> <p>More information can also be found here:</p> <ul> <li><a href="http://aspnet.4guysfromrolla.com/articles/081402-1.aspx" rel="noreferrer">http://aspnet.4guysfromrolla.com/articles/081402-1.aspx</a></li> <li><a href="http://aspnet.4guysfromrolla.com/articles/082102-1.aspx" rel="noreferrer">http://aspnet.4guysfromrolla.com/articles/082102-1.aspx</a></li> </ul> <p>and of course on <a href="http://geekswithblogs.net/rashid/archive/2007/08/11/Loading-UserControl-Dynamically-in-UpdatePanel.aspx" rel="noreferrer">the website that holds the example code</a> I used here.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload