如何在会话中存储和检索动态表。 [英] How can store and retrieve a dynamic table in session.

查看:64
本文介绍了如何在会话中存储和检索动态表。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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();
    }

推荐答案

Quote:

我使用带有c#的Asp.net创建了一个动态表。现在我想在会话中存储这个动态创建的表,我该怎么做?

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?





你不在会话中存储HTML表。相反,您将存储表中的行数和列数,以便根据先前的行数和列数在每个回发上重新创建表。这是一个简单的例子:



ASPX:



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

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;

    }
}


读取此在ASP.NET中探索会话 [ ^ ]


这篇关于如何在会话中存储和检索动态表。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆