How to dynamically add a table row with input fields in .NET.

C# using Visual Studio 2005…

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 
{
    // How many rows when the page initially loads.
    static int tDefaultRow_Count = 10;
    // Max number of rows.
    static int tMaxRows = 50;
 
    // Do not modify the following.
    private int tUserAddedRowCount = 0;
    static TextBox[] TextBox_Item = new TextBox[tMaxRows];
    static TextBox[] TextBox_Desc = new TextBox[tMaxRows];
    static TextBox[] TextBox_Quantity = new TextBox[tMaxRows];
    static TextBox[] TextBox_Price = new TextBox[tMaxRows];
    static TextBox[] TextBox_Total = new TextBox[tMaxRows];
 
    protected void Page_Load(object sender, EventArgs e)
    {
        tUserAddedRowCount = (ViewState["tUserAddedRowCount"] != null) ? int.Parse(ViewState["tUserAddedRowCount"].ToString()) : 0;
        //Response.Write("User Rows: " + tUserAddedRowCount + "\r\n");
 
        // Recreate the extra rows created by the user on postback
        // events (required to maintain state).
        Add_Table_Rows(tUserAddedRowCount + tDefaultRow_Count);
    }
 
    protected void Button1_Click(object sender, EventArgs e)
    {
            // User clicked "add a row" button.
 
            Add_Table_Rows(1);
            ViewState["tUserAddedRowCount"] = ++tUserAddedRowCount;
    }
 
    protected void Button2_Click(object sender, EventArgs e)
    {
        // User clicked "Submit" button.
 
        for (int x = 0; x < tDefaultRow_Count + tUserAddedRowCount; x++)
        {
 
            if (TextBox_Item[x].Text.ToString().Length > 0) 
            { 
                Response.Write(TextBox_Item[x].Text.ToString());
                Response.Write(TextBox_Desc[x].Text.ToString());
                Response.Write(TextBox_Quantity[x].Text.ToString());
                Response.Write(TextBox_Price[x].Text.ToString());
                Response.Write(TextBox_Total[x].Text.ToString());
            }
        }
    }
 
    private void Add_Table_Rows(int NumberOfRows)
    {
        for (int x = 0; x < NumberOfRows; x++)
        {
            TableRow myRow = new TableRow();
            Table1.Rows.Add(myRow);
 
            TableCell myCell0 = new TableCell();
            myRow.Cells.Add(myCell0);
            TextBox myTextbox0 = new TextBox();
            TextBox_Item[x] = myTextbox0;
            myCell0.Controls.Add(TextBox_Item[x]);
 
            TableCell myCell1 = new TableCell();
            myRow.Cells.Add(myCell1);
            TextBox myTextbox1 = new TextBox();
            TextBox_Desc[x] = myTextbox1;
            myCell1.Controls.Add(TextBox_Desc[x]);
 
            TableCell myCell2 = new TableCell();
            myRow.Cells.Add(myCell2);
            TextBox myTextbox2 = new TextBox();
            TextBox_Quantity[x] = myTextbox2;
            myCell2.Controls.Add(TextBox_Quantity[x]);
 
            TableCell myCell3 = new TableCell();
            myRow.Cells.Add(myCell3);
            TextBox myTextbox3 = new TextBox();
            TextBox_Price[x] = myTextbox3;
            myCell3.Controls.Add(TextBox_Price[x]);
 
            TableCell myCell4 = new TableCell();
            myRow.Cells.Add(myCell4);
            TextBox myTextbox4 = new TextBox();
            TextBox_Total[x] = myTextbox4;
            myCell4.Controls.Add(TextBox_Total[x]);
 
        }
    }
}

Comments RSS


Leave a comment:


Blog | Contact | Gallery | Links | Sandbox | Social Networking | Weather | Web Design


Copyright © 1997-2009 KCSH. All rights reserved.