Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could store the bare minimum of what you need to know to recreate the controls in a collection held in session. Session is available during the init phases of the page.</p> <p>Here is an example for you. It consists of: </p> <p>Default.aspx, cs<br> - panel to store user controls<br> - "Add Control Button" which will add a user control each time it is clicked</p> <p>TimeTeller.ascx, cs<br> - has a method called SetTime which sets a label on the control to a specified time.</p> <p>Default.aspx </p> <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DynamicControlTest._Default" %&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;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;div&gt; &lt;asp:Panel ID="pnlDynamicControls" runat="server"&gt; &lt;/asp:Panel&gt; &lt;br /&gt; &lt;asp:Button ID="btnAddControl" runat="server" Text="Add User Control" onclick="btnAddControl_Click" /&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Default.aspx.cs: </p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace DynamicControlTest { public partial class _Default : System.Web.UI.Page { Dictionary&lt;string, string&gt; myControlList; // ID, Control ascx path protected void Page_Load(object sender, EventArgs e) { } protected override void OnInit(EventArgs e) { base.OnInit(e); if (!IsPostBack) { myControlList = new Dictionary&lt;string, string&gt;(); Session["myControlList"] = myControlList; } else { myControlList = (Dictionary&lt;string, string&gt;)Session["myControlList"]; foreach (var registeredControlID in myControlList.Keys) { UserControl controlToAdd = new UserControl(); controlToAdd = (UserControl)controlToAdd.LoadControl(myControlList[registeredControlID]); controlToAdd.ID = registeredControlID; pnlDynamicControls.Controls.Add(controlToAdd); } } } protected void btnAddControl_Click(object sender, EventArgs e) { UserControl controlToAdd = new UserControl(); controlToAdd = (UserControl)controlToAdd.LoadControl("TimeTeller.ascx"); // Set a value to prove viewstate is working ((TimeTeller)controlToAdd).SetTime(DateTime.Now); controlToAdd.ID = Guid.NewGuid().ToString(); // does not have to be a guid, just something unique to avoid name collision. pnlDynamicControls.Controls.Add(controlToAdd); myControlList.Add(controlToAdd.ID, controlToAdd.AppRelativeVirtualPath); } } } </code></pre> <p>TimeTeller.ascx</p> <pre><code>&lt;%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TimeTeller.ascx.cs" Inherits="DynamicControlTest.TimeTeller" %&gt; &lt;asp:Label ID="lblTime" runat="server"/&gt; </code></pre> <p>TimeTeller.ascx.cs </p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace DynamicControlTest { public partial class TimeTeller : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) { } public void SetTime(DateTime time) { lblTime.Text = time.ToString(); } protected override void LoadViewState(object savedState) { base.LoadViewState(savedState); lblTime.Text = (string)ViewState["lblTime"]; } protected override object SaveViewState() { ViewState["lblTime"] = lblTime.Text; return base.SaveViewState(); } } } </code></pre> <p>As you can see, I still have to manage the internal viewstate of my user control, but the viewstate bag is being saved to the page and handed back to the control on postback. I think it is important to note that my solution is very close to David's. The only major difference in my example is that it's using session instead of viewstate to store the control info. This allows things to happen during the initialization phase. It is important to note that this solution takes up more server resources, therefore it may not be appropriate in some situations depending on your scaling strategy. </p>
    singulars
    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.
 

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