C# add rows on_click event of button

I want to be able to add a row to tblMenu every time I click the button. The button adds a row the first time it is clicked but never again no matter how many times I click it. Thanks in advance.
protected void btnAddRow_Click(object sender, EventArgs e)
    {
        TableRow tRowX = new TableRow();
        tRowX.ID = "tRowX_" + tblMenu.Rows.Count.ToString();
        TableCell tCelX = new TableCell();
        tCelX.Text = tRowX.ID.ToString();
        tRowX.Cells.Add(tCelX);
        tblMenu.Rows.Add(tRowX);
}

Open in new window

Chuck98RT10Asked:
Who is Participating?
 
itzdarcConnect With a Mentor Commented:
Here.
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Collections.Generic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
    
    List<TableRow> rowList;
    
    protected void DisplayTable()
    {
        //Run through the row list, and add each one to the Table's Row Collection.
        foreach (TableRow tr in rowList)
            tblMenu.Rows.Add(tr);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["CSRL"] != null && Request.QueryString["CSRL"].ToString() == "1" && Session["rowList"] != null) Session.Remove("rowList");
        
        if (Session["rowList"] == null)
        {
            //No row list, Create it.
            rowList = new List<TableRow>();
        }
        else
        {
            rowList = (List<TableRow>)Session["rowList"];
        }
        //Recreate the table. (Prevents loosing data already entered into textboxes.)
        DisplayTable();
        
        if (Page.IsPostBack == true) return;
 
    }
 
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Write(tblMenu.Rows.Count.ToString());
        TableRow tRowX = new TableRow();
        TableCell tCelX = new TableCell();
        
        tRowX.ID = "tRowX_" + tblMenu.Rows.Count.ToString();
        tCelX.ID = "tCelX_" + tblMenu.Rows.Count.ToString();
        tCelX.Text = tRowX.ID.ToString();
        
        tRowX.Cells.Add(tCelX);
        tblMenu.Rows.Add(tRowX);
 
        rowList.Add(tRowX);
        Session["rowList"] = rowList;
    }
 
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    
    <asp:Table ID="tblMenu" runat="server" EnableViewState="true">
    
    </asp:Table>
    
    <asp:Button ID="btnAdd" runat="server" Text="Add!" OnClick="btnAdd_Click" />
    </div>
    </form>
</body>
</html>

Open in new window

0
 
Chuck98RT10Author Commented:
FYI the table is sitting inside a panel but that should matter.
0
 
itzdarcCommented:
Dynamically created controls apparently don't persist properly.

http://code2dotnet.blogspot.com/2007/09/add-rows-to-asp-table-web-control.html

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
Chuck98RT10Author Commented:
Wow. Thanks for the find. I figured this was gonna be easy so I originally gave it 125 points. I bumped it up to 500.
0
 
itzdarcCommented:
I would be advised though, using the session object stores the data in memory on the server for the life of the session.  That means that if you do this for 4 tables, and you have 4 users that each load all 4 tables at the same time, there are 16 instances of a table stored in the *servers* memory.  However, the Session / Application state objects are the only way to store an object for the purposes you're wanting.  You can use the ViewState collection to store serialized (text) representations of the tables, and then load them back later.  This puts most of the stress for maintaining the state of the tables on the client as apposed to relying entirely on the server.  Also, this would help preserve the state of the objects should the session expire prematurely.
0
 
Chuck98RT10Author Commented:
I don't believe it. It worked!  Thank you Itzdarc. Very well written and simple code.  If I could change your award points from 500 to a gazillion I'd do it. Mucho grats!!!!!!!!!!!!!!!!!!!!!!!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.