A cleaner way to add controls to a page dynamically

This is a cleaner way to add controls to a page dynamically, using ASP.Net tables, adding asp.net table columns and asp.net table rows:

Use this in the markup:
<asp:Table id=”tblDynamic” runat=”server”></asp:Table>

Then this in the code behind:

for ( int i = 0; i < 5; i++ )
{
    TableRow tr = new TableRow(); 

    // Create column 1 
    TableCell td1 = new TableCell(); 

    // Create a label control dynamically 
    Label _label = new Label(); 
    _label.ID = “lbl” + i.ToString(); 
    _label.Text = “Enter Value ” + i.ToString(); 

    // Add control to the table cell 
    td1.Controls.Add(_label);

    // Create column 2 
    TableCell td2 = new TableCell(); 
    TextBox _text = new TextBox(); 
    _text.ID = “txt_” + i.ToString();

    // Add control to the table cell 
    td2.Controls.Add(_text);

    // Add cell to the row 
    tr.Cells.Add(td1); 
    tr.Cells.Add(td2);

    // Add row to the table. 
    tblDynamic.Rows.Add(tr);
}

Adding controls dynamically

Adding Controls Dynamically to an ASP.Net page

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        // max = # of items in the list
        int max = 5;

        if (max > 0)
        {
            Literal topTbl = new Literal();
            topTbl.Text = “<table border=’1′ cellpadding=’1′ cellspacing=’1′>”;
            thing.Controls.Add(topTbl);

            for (int i = 0; i < max; i++)
            {
                Literal leftRow = new Literal();
                leftRow.Text = “<tr><td>”;
                thing.Controls.Add(leftRow);

                Label label1 = new Label();
                label1.ID = “xxx” + i.ToString();
                label1.Text = “hello world” + i.ToString() + ” “;
                thing.Controls.Add(label1);

                Literal midRow = new Literal();
                midRow.Text = “</td><td>”;
                thing.Controls.Add(midRow);

                CheckBox cb = new CheckBox();
                cb.ID = “yyy” + i.ToString();
                thing.Controls.Add(cb);

                midRow = new Literal();
                midRow.Text = “</td><td>”;
                thing.Controls.Add(midRow);

                Label label2 = new Label();
                label2.Text = ” {” + i.ToString() + “} “;
                label2.ID = “zzz” + i.ToString();
                thing.Controls.Add(label2);

                Literal rightRow = new Literal();
                rightRow.Text = “</td></tr>”;
                thing.Controls.Add(rightRow);
            }

            Literal bottomTbl = new Literal();
            bottomTbl.Text = “</table>”;

            thing.Controls.Add(bottomTbl);
        }
    }

}

Advertisements