简体   繁体   中英

Table Control Data persistence ASP.net C#

I'm creating an asp.net/c# website and I'm having trouble with data persistence of a table control,

I have to add rows to the table dynamically from a set of textboxes several times, so, as is well known the http is stateless I'm saving the table content in the Session object every time I add a new row, then in the page_load event I try to retrieve the data from Session and adding it to the table, but it doesn't load again. I've debugged my code, and the table row count is increased in every postback, so I know my method works, but I have no idea why it does not render the table properly

Here's a simplified version of my code which shows my problem (it also does not work LOL)

<div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    <asp:Table ID="Table1" runat="server" ViewStateMode="Disabled" EnableViewState="False">
        <asp:TableRow>
            <asp:TableHeaderCell>
                Nombres
            </asp:TableHeaderCell>
        </asp:TableRow>
    </asp:Table>
</div>

protected void Page_Load(object sender, EventArgs e)
{
        if (IsPostBack)
        {
            if (Session["mtable"] != null)
            {
                Table1 = (Table)Session["mtable"];
            }
        }
}

protected void Button1_Click(object sender, EventArgs e)
{
        TableCell tc = new TableCell();
        tc.Text = TextBox1.Text;
        TableRow tr = new TableRow();
        tr.Cells.Add(tc);
        Table1.Rows.Add(tr);
        Session["mtable"] = Table1;
}

Please help, thanks

Enabling viewstate would not change anything : 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.

You have to rebuild your table. This way for example :

List<String> data = new List<String>();

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["mtable"] != null)
    {
        data = (List<String>)Session["mtable"];
    }
    if (!IsPostBack) BuildTable();
}

protected void BuildTable()
{
    if (Session["mtable"] != null)
    {
        foreach (String s in data)
        {
            TableCell tc = new TableCell();
            tc.Text = s;
            TableRow tr = new TableRow();
            tr.Cells.Add(tc);
            Table1.Rows.Add(tr);
        }
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    data.Add(TextBox1.Text);
    Session["mtable"] = data;
    BuildTable();
}

If you have complex data, use a gridview and bind it to a datatable object that you can save in a Session variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM