Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have created a dynamic table using Asp.net with c#. Now I want to store this dynamically created table in the session, How can i do that?

Thanks.


What I have tried:

public partial class WebForm1 : System.Web.UI.Page
    {
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    public void CreateRuntime_Table()
    {
        int tblRows = int.Parse(txtrow.Text);
        int tblCols = int.Parse(txtcol.Text);

        Table tbl = new Table();
        tbl.BorderWidth = 3;
        tbl.BorderStyle = BorderStyle.Solid;
        tbl.ID = "myTable";

        for (int i = 1; i <= tblRows; i++)
        {

            TableRow tr = new TableRow();
            for (int j = 1; j <= tblCols; j++)
            {
                TableCell tc = new TableCell();
                TextBox txtbox = new TextBox();
                txtbox.Text = "Test Row:" + i + "Test Col:" + " " + j;
                //Add the control to the table cell
                tc.Controls.Add(txtbox);
                tr.Controls.Add(tc);
            }

            tbl.Rows.Add(tr);
        }

        form1.Controls.Add(tbl);
    }

    protected void Unnamed_Click(object sender, EventArgs e)
    {
        CreateRuntime_Table();
    }
Posted
Updated 30-Aug-18 3:47am
Comments
F-ES Sitecore 30-Aug-18 4:17am    
Have you tried

Session["MyTable"] = tbl;
Syf AK 30-Aug-18 4:23am    
Yes i had tried this but still not working

To store in Session:
Session["table"] = tbl as Table;

To Retrive:
Table table = Session["table"] as Table;

After that try to setting colspan for the same table but the table is not coming across the post back:
for (int i = 1; i <= colspan; i++)
{
TableCell TC = new TableCell();
TC.ColumnSpan = colspan;
}

Quote:
I have created a dynamic table using Asp.net with c#. Now I want to store this dynamically created table in the session, How can i do that?


You don't store the HTML Table in Session. Instead you will store the rows and columns count from your Table so you can recreate your Table on each and every postbacks based from the previous rows and columns count. Here's a quick example:

ASPX:
HTML
<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
    <title>Dynamic Adding of Rows in ASP Table Demo</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Add New Row" />
    </form>
    </body>
</html>


CODE BEHIND:
C#
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default1 : System.Web.UI.Page
{

    //A global variable that will hold the current number of Rows
    //We set the values to 1 so that it will generate a default Row when the page loads
    private int numOfRows = 1;

    protected void Page_Load(object sender, EventArgs e)
    {
        //Generate the Rows on Initial Load
        if (!Page.IsPostBack){
            GenerateTable(numOfRows);
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        if (Session["RowsCount"] != null)
        {
            numOfRows = Convert.ToInt32(Session["RowsCount"].ToString());
            GenerateTable(numOfRows);
        }
    }


    private void SetPreviousData(int rowsCount, int colsCount)
    {
        Table table = (Table)Page.FindControl("Table1");
        if (table != null)
        {
            for (int i = 0; i < rowsCount; i++)
            {
                for (int j = 0; j < colsCount; j++)
                {
                    //Extracting the Dynamic Controls from the Table
                    TextBox tb = (TextBox)table.Rows[i].Cells[j].FindControl("TextBoxRow_" + i + "Col_" + j);
                    //Use Request objects for getting the previous data of the dynamic textbox
                    tb.Text = Request.Form["TextBoxRow_" + i + "Col_" + j];
                }
            }
        }
    }

 

    private void GenerateTable(int rowsCount)
    {
        //Creat the Table and Add it to the Page

        Table table = new Table();
        table.ID = "Table1";
        Page.Form.Controls.Add(table);

        //The number of Columns to be generated
        const int colsCount = 3;//You can changed the value of 3 based on you requirements

        // Now iterate through the table and add your controls

        for (int i = 0; i < rowsCount; i++)
        {
            TableRow row = new TableRow();
            for (int j = 0; j < colsCount; j++)
            {
                TableCell cell = new TableCell();
                cell.ColumnSpan = colsCount;
                TextBox tb = new TextBox();

                // Set a unique ID for each TextBox added
                tb.ID = "TextBoxRow_" + i + "Col_" + j;
                // Add the control to the TableCell
                cell.Controls.Add(tb);

                // Add the TableCell to the TableRow
                row.Cells.Add(cell);
            }
			
            // And finally, add the TableRow to the Table
            table.Rows.Add(row);
        }


        //Set Previous Data on PostBacks
        SetPreviousData(rowsCount, colsCount);
        //Sore the current Rows Count in ViewState
        rowsCount++;

        Session["RowsCount"] = rowsCount;

    }
}
 
Share this answer
 
have a read of this Exploring Session in ASP.NET[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900