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

Asked

Viewed 1,187 times

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 the fields to add a new line to the Table. The insersion part in the table works very well, but only once the second time I try to add another row to the table the previous one disappears and this new row is 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 am entering the data in the original Table and not in the Table of the postback page. The funny thing is that I did the same with a Listbox and it worked, I managed to add more than one item.

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

2 answers

5


Zignd, look over here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.table(v=vs.110). aspx

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 lines and cells added to Table have to be reconstructed at each postback.

That is, you have to store the content of the lines you insert into the table somewhere else, and add them all at some point, probably in Page_load. For this you have some options:

  • Viewstate - Advantage of staying on the page itself, but beware as it can make loading very slow
  • Session - Keep the data on the server, usually in memory, that is, access is fast. However, if it is too much data can cause you problems.
  • Database, etc.

1

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.