Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
See more:
how to dynamic create table row on each button click in c#?
Posted
Comments
Karthik_Mahalingam 16-Jan-14 3:25am    
Pls provide more details.

click[^]
 
Share this answer
 
on .aspx page I create a placeholder that will contain a Table.

HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication2.WebForm2" EnableViewState="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

     <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
    </div>
   <asp:PlaceHolder ID="resultHolder" runat="server"></asp:PlaceHolder>
    </form>
</body>
</html>


There after define logic on code behind page

C#
using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        static Table tableResult;
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                tableResult = new Table();
                tableResult.GridLines = GridLines.Both;
                TableHeaderRow dr = new TableHeaderRow();
                TableHeaderCell dc = new TableHeaderCell();
                dc.Text = "Name";
                dr.Cells.Add(dc);
                tableResult.Rows.Add(dr);
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            TableRow dr = new TableRow();
            TableCell dc = new TableCell();
            dc.Text = "Sandeep";
            dr.Cells.Add(dc);
            tableResult.Rows.Add(dr);
            resultHolder.Controls.Add(tableResult);
        }
       
    }
    
}


This is basci logic to add a row on button click you can change dynamically text for cell as you want.
Thanks
 
Share this answer
 
Form.aspx
XML
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                                    <ContentTemplate>
                                                        <asp:Panel ID="panelTable" runat="server">
                                                        </asp:Panel>
                                                        <span class="style1">
                                                            <asp:Button ID="btnAddRow" runat="server" Text="Add More.." OnClick="btnAddRow_Click" />
                                                    </ContentTemplate>
                                                </asp:UpdatePanel>


Form.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Drawing;
using System.IO;
using BusinessLayerLogic.Registration;
using System.Text;
using System.Configuration;
using System.Data;


public partial class Form : System.Web.UI.Page
{
    #region "Variable"
    //A global variable that will hold the current number of Rows

    //We set the values to 2 so that it will generate a default Row when the page loads

    private int numOfRows = 2;
    #endregion


    #region "PageLoad"
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            //GenerateTable(numOfRows);
            if (!IsPostBack)
            {
                GenerateTable(numOfRows);

            }
            
            
            #endregion
        }
        catch (Exception ex)
        {
            
        }
            

    }
    #endregion


    #region "Button Click Event"
    

    protected void btnAddRow_Click(object sender, EventArgs e)
    {
        if (ViewState["RowsCount"] != null)
        {

            numOfRows = Convert.ToInt32(ViewState["RowsCount"].ToString());

            GenerateTable(numOfRows);

        }

    }

    
    #endregion


    



    #region "Methods/Functions"
    
    //Dyanamically gets the data of work experiance from previos table and set it in the newly Genrated Table

    private void SetPreviousData(int rowsCount, int colsCount)
    {
        rowsCount--;
        try
        {
            Table table = (Table)Page.FindControl("Table1");


            if (table != null)
            {

                for (int i = 0; i < rowsCount; i++)
                {

                    for (int j = 0; j < colsCount; j++)
                    {

                        //Extracting the Dynamic Controls from the Table

                        TextBox tb = (TextBox)table.Rows[i].Cells[j].FindControl("TextBoxRow_" + i + "Col_" + j);

                        //Use Request objects for getting the previous data of the dynamic textbox

                        //if (j == 0)
                        //{
                        //    int Sno = i;
                        //    tb.Text = Convert.ToString(++Sno);
                        //}
                        //else
                        tb.Text = Request.Form["TextBoxRow_" + i + "Col_" + j];

                    }

                }


            }
        }
        catch (Exception ex)
        {

            throw ex;
        }


    }


    //Genrates the table dyanmically for the Table Work Experiance and add the to the page
    private void GenerateTable(int rowsCount)
    {
        Table table = new Table();

        table.ID = "Table1";

        panelTable.Controls.Add(table);



        //The number of Columns to be generated

        const int colsCount = 6;


        //adding the head Row to the Table

        TableHeaderRow headRow = new TableHeaderRow();
        headRow.BackColor = Color.Gray;
        headRow.ForeColor = Color.WhiteSmoke;
        headRow.Height = 33;

        TableHeaderCell headCell = new TableHeaderCell();
        headCell.Text = "S.No";
        headCell.Width = 10;
        headRow.Cells.Add(headCell);

        TableHeaderCell headCell1 = new TableHeaderCell();
        headCell1.Text = "Name of Ogranisation";
        headRow.Cells.Add(headCell1);

        TableHeaderCell headCell6 = new TableHeaderCell();
        headCell6.Text = "Designation & Key Area of Experience";
        headRow.Cells.Add(headCell6);

        TableHeaderCell headCel3 = new TableHeaderCell();
        headCel3.Text = "Key Achievements";
        headRow.Cells.Add(headCel3);

        TableHeaderCell headCell4 = new TableHeaderCell();
        headCell4.Text = "Experience From";
        headRow.Cells.Add(headCell4);

        TableHeaderCell headCell5 = new TableHeaderCell();
        headCell5.Text = "Experience To";
        headRow.Cells.Add(headCell5);

        table.Rows.Add(headRow);

        // Now iterate through the table and add your controls
        for (int i = 0; i < rowsCount; i++)
        {

            TableRow row = new TableRow();

            for (int j = 0; j < colsCount; j++)
            {

                TableCell cell = new TableCell();

                TextBox tb = new TextBox();


                // Set a unique ID for each TextBox added

                tb.ID = "TextBoxRow_" + i + "Col_" + j;
                tb.Height = 25;
                if (j == 2)
                {
                    tb.Width = 228;
                }

                // Add the control to the TableCell

                cell.Controls.Add(tb);

                // Add the TableCell to the TableRow

                row.Cells.Add(cell);

            }



            // And finally, add the TableRow to the Table

            table.Rows.Add(row);

        }


        //Set Previous Data on PostBacks

        SetPreviousData(rowsCount, colsCount);



        //Store the current Rows Count in ViewState

        rowsCount++;

        ViewState["RowsCount"] = rowsCount;

    }

    #endregion

}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900