Add New Rows to an existing ASP.Net Table

kishan66
kishan66 used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Hi all,

i'm trying to add new rows by using AppendRows() on another Button click event.

But, when the event is fired , the table which was created initially is gone.. there fore no more rows are added
is there any way i can retain the initial table though i click "Add More" button


 
private void AppendRows()
{
 for (int rowCtr = 1; rowCtr <= 3; rowCtr++)
{
   ct = tbl.Rows.Count;
    TableRow trow = new TableRow();
  tbl.Rows.Add(trow);
   for (int colCtr = 1; colCtr <= 3; colCtr++)
  {
      TableCell tCell = new TableCell();
       trow.Cells.Add(tCell);
      // Mock up a product ID.
       string prodID = rowCtr + "_" + colCtr;
        // Add a literal text as control.
       tCell.Controls.Add(new LiteralControl("Buy: "));
      }
  }
}

Open in new window

Commented:
If you're creating the table dynamically, you will need to create it again on each postback, then add the new rows.

Author

Commented:
rick_gwu,

I have values entered by User in Intial table .. to which i have to add more rows, if needed.
Calling the CreateTable in PostBack will remove all the values from the Intial Table

Commented:
You could save the table in session variable, repopulate it on postback, then add the new rows.
On postback, you could try recreating the controls and table in the Init event so that the dynamic controls can load their postback data.

protected void Page_Init(object sender, EventArgs e)
{
     if(IsPostBack)
     {
         CreateDynamicTable();
     }
}

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial