How to edit data from a Table after postback in ASP.NET?

1

I have a Table in a Web Form in ASP.NET and just below in the same form I added a TextBox in which you fill in the fields to add a new row to the Table. The insertion part in the table works fine, but only once the second time I try to add another row to the table the previous one disappears and this new row stays in its place and so on.

ASP.NET

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Table ID="TableAutores" runat="server" Caption="<b>Autores</b>" BorderWidth="2px" BorderStyle="Solid">
                <asp:TableHeaderRow runat="server" Font-Bold="true">
                    <asp:TableHeaderCell>Nome</asp:TableHeaderCell>
                    <asp:TableHeaderCell>Sobrenome</asp:TableHeaderCell>
                </asp:TableHeaderRow>
                <asp:TableRow>
                    <asp:TableCell>AAA</asp:TableCell>
                    <asp:TableCell>AAA</asp:TableCell>
                </asp:TableRow>
                <asp:TableRow>
                    <asp:TableCell>BBB</asp:TableCell>
                    <asp:TableCell>BBB</asp:TableCell>
                </asp:TableRow>
                <asp:TableRow>
                    <asp:TableCell>CCC</asp:TableCell>
                    <asp:TableCell>CCC</asp:TableCell>
                </asp:TableRow>
            </asp:Table>
            <br />
            <asp:Label Text="Nome" runat="server" />
            <asp:TextBox ID="TextBoxNome" runat="server" />
            <asp:Label Text="Sobrenome" runat="server" />
            <asp:TextBox ID="TextBoxSobrenome" runat="server" />
            <asp:Button ID="ButtonAdicionar" Text="Adicionar autor" runat="server" OnClick="ButtonAdicionar_Click" />
        </div>
    </form>
</body>
</html>

C #

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default : System.Web.UI.Page
{
    protected void ButtonAdicionar_Click(object sender, EventArgs e)
    {
        TableCell nome = new TableCell();
        TableCell sobrenome = new TableCell();
        nome.Text = TextBoxNome.Text;
        sobrenome.Text = TextBoxSobrenome.Text;
        TableRow linha = new TableRow();
        linha.Cells.Add(nome);
        linha.Cells.Add(sobrenome);
        TableAutores.Rows.Add(linha);
    }
}

Apparently I'm inserting the data into the original Table and not into the Table of the postback page. The funny thing is that I did the same with a ListBox and it worked, I was able to add more than one item.

What should I do to be able to add a row in the Table of the postback page (if this is the correct term to use)? Is there an attribute that I need to modify?

    
asked by anonymous 19.12.2013 / 16:07

2 answers

5

Zignd, see here: link

  

It is important to remember that any programmatic addition or modification of table rows or cells will not persist across posts to the server. This is because table rows and cells are controls of their own, and not properties of the Table control. To persist any changes to the table, rows and cells must be reconstructed after each postback.

Translating, the rows and cells added in the Table have to be rebuilt on every postback.

That is, you have to store the contents of the lines you insert into the Table somewhere else, and add all of them at some point, probably in the Page_Load. For this you have a few options:

  • Viewstate - Advantage of staying on the page itself, but be careful as it can make uploading very slow
  • Session - Keeps the data in the server, usually in memory, ie access is fast. But if it's too much data it can also cause you problems.
  • Database, etc.
19.12.2013 / 17:22
1

You need to rebuild your table with this new value which may imply creating it entirely in the code behind. Another option that might make your job easier is to use a gridview, store the datasource in viewstate, and bind () after the postback.

    
03.04.2014 / 15:03