RSS

Monthly Archives: May 2013

How to add Controls Dynamically and Have Them Persist Through Postback

So the need arises and you need to add controls on demand in my case it is so that a user can add multiple accounts to pay by clicking a button. Here is how to do it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    private const int controlCount = 1;

    protected void Page_Init(object sender, EventArgs e)
    {
        
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            foreach (HtmlGenericControl item in MyControlState)
            {
                PlaceHolder1.Controls.Add(item);
            }
        }
    }

    private int _CurrentControlcount
    { 
        get
        {
            int _currentControlcount = object.Equals(Session["_currentControlcount"], null) ? controlCount : (int)Session["_currentControlcount"];
            if (Object.Equals(_currentControlcount, null))
            {
                _currentControlcount = new int();
                _currentControlcount = 0;
                Session["_currentControlcount"] = _currentControlcount;
                
            }
            return _currentControlcount;
        }        
        set
        {
             Session["_currentControlcount"] = value;
        }
    }

    private List<HtmlGenericControl> MyControlState
    {
        get
        {
            List<HtmlGenericControl> myControlState = (List<HtmlGenericControl>)Session["myControlState"];
            if (object.Equals(myControlState,null))
            {
                myControlState = new List<HtmlGenericControl>();
            }

            return myControlState;
        }
        set
        {
            Session["myControlState"] = value;
        }
        
    }

    protected void btnAddControls_Click(object sender, EventArgs e)
    {
        int count = MyControlState.Count();
        HtmlGenericControl myGenericUL = new HtmlGenericControl("ul");
        HtmlGenericControl myGenericLI = new HtmlGenericControl("li");
        TextBox tbx = new TextBox() { Text = string.Format("Textbox{0}", count), ID = string.Format("Textbox{0}", count) };
        myGenericLI.Controls.Add(tbx);
        myGenericUL.Controls.Add(myGenericLI);
        PlaceHolder1.Controls.Add(myGenericUL);
        List<HtmlGenericControl> setup = MyControlState;
        setup.Add(myGenericUL);
        MyControlState = setup;
        _CurrentControlcount = _CurrentControlcount + 1;
    }

}

and the html

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!-- use the default masterpage from microsoft or whatever you like -->
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Welcome to ASP.NET!
    </h2>
    <p>
        To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
    </p>
    <p>
        You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&amp;clcid=0x409"
            title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
    </p>
    
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
    <asp:PlaceHolder ID="PlaceHolder1" runat="server" >

    </asp:PlaceHolder>
    <br />
    <asp:Button ID="btnAddControls" runat="server" Text="Add Control" 
        onclick="btnAddControls_Click" />

</asp:Content>

now I know that the code is rough and I am touching it up as we speak i really meant to hit draft not publish but it will be ok

 
Leave a comment

Posted by on May 20, 2013 in Uncategorized