Avatar of kishan66
kishan66Flag for United States of America

asked on 

Add New Rows to an existing ASP.Net Table

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

C#ASP.NET

Avatar of undefined
Last Comment
Alfred A.
Avatar of kishan66
kishan66
Flag of United States of America image

ASKER

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

Avatar of Rick
Rick

If you're creating the table dynamically, you will need to create it again on each postback, then add the new rows.
Avatar of kishan66
kishan66
Flag of United States of America image

ASKER

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
Avatar of Rick
Rick

You could save the table in session variable, repopulate it on postback, then add the new rows.
ASKER CERTIFIED SOLUTION
Avatar of Alfred A.
Alfred A.
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo