We help IT Professionals succeed at work.
Get Started

Add New Rows to an existing ASP.Net Table

kishan66
kishan66 asked
on
329 Views
Last Modified: 2012-05-11
Hi,
Using (ASP Button or HyperLink)  onClick -> Add New Rows to an existing ASP table

I have create a table on button click (pls see code under code section).

Now, when ever user clicks on "Add More" , 4 Rows have to be appended to the existing table.

pls suggest me using javascript or ASP.net itself

ASP.net 2.0, C#.net

ASPX code
---------
<div>
    Rows: <asp:TextBox ID="txtRows" runat="server" Width="30px"> </asp:TextBox> <br />
    Cols: &nbsp;<asp:TextBox ID="txtCols" runat="server" Width="30px"></asp:TextBox>
     <asp:Button ID="btnGenerate" OnClick="btnGenerate_Click" runat="server" Text="Generate" />&nbsp;<br /> <br />
     <asp:Button ID="btnMoreRows" OnClick="btnMoreRows_Click" runat="server" Text="Add More" />&nbsp;<br /> <br />
	<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>  


ASPX.CS code
------------
ASP-Table Creation Function:
protected void btnGenerate_Click(object sender, EventArgs e)
{
    CreateDynamicTable();
}
 private void CreateDynamicTable()
{
    PlaceHolder1.Controls.Clear();
    // Fetch the number of Rows and Columns for the table 
    // using the properties
    int tblRows = 4;
    int tblCols = 4;
    // Create a Table and set its properties 
    //tb1 = new Table();
    // Add the table to the placeholder control
    PlaceHolder1.Controls.Add(tbl);
    // Now iterate through the table and add your controls 
    for (int i = 0; i < tblRows; i++)
    {
        TableRow tr = new TableRow();
        for (int j = 0; j < tblCols; j++)
        {
            TableCell tc = new TableCell();
            TextBox txtBox = new TextBox();
            txtBox.Text = "RowNo:" + i + " " + "ColumnNo:" + " " + j;
            // Add the control to the TableCell
            tc.Controls.Add(txtBox);
            // Add the TableCell to the TableRow
            tr.Cells.Add(tc);
        }
        // Add the TableRow to the Table
        tbl.Rows.Add(tr);
    }            
}

Open in new window

Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE