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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

itzdarcCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.