如何将数据表或数据集导入会话?

时间:2016-02-18 01:04:30

标签: c# asp.net session

如何将数据表或数据集导入会话?当客户端连接到应用程序时,我希望客户端在超时前与gridview保持相同的会话30分钟。你会怎么做?我看了几个例子 在W3 schoolsasp.net-Tutorials

但我不能开始,我不确定Session DataTable或DataSet。这是我第一次使用Session与ASP.Net - 任何反馈或提示真的很感激!

using System;
using DBComponentsLibrary;
using DBComponentsLibrary.NameDataSetTableAdapters;
using System.Web.UI.WebControls;

namespace Name
{
    public partial class TestWebFormView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                DataBind();
                loadGridData();
        }
        private void loadGridData()
        {
            NameTableAdapter TA = new NameTableAdapter();
            NameDataSet rds = new NameDataSet();
            NameDataSet.NameDataTable Rdt = new NameDataSet.NameDataTable();
            TA.Fill(Rdt);
            GridView.DataSource = Rdt;
            GridView.DataBind();
            GridView.Rows[0].Cells[0].Visible = true;
        }
        protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView.PageIndex = e.NewPageIndex;
            GridView.DataBind();
        }
        protected void btnLastPage_Click(object sender, EventArgs e)
        {
            if (GridView.PageCount > 0)
            {
                GridView.SetPageIndex(GridView.PageCount - 1);
            }
        }
        protected void btnExport_Click(object sender, EventArgs e)
        {
            //Will be implemented soon.
        }
    }
}

2 个答案:

答案 0 :(得分:2)

请找到代码,希望它能给你一个开始:

        if (Session["SeesionName"] != null)
        {
            DataTable dt = (DataTable)Session["SeesionName"];
            DataRow row = dt.NewRow();
            row["SNO"] = dt.Rows.Count + 1;
            row["EmpId"] = empId;
            dt.Rows.Add(row);
            gdSource.DataSource = dt;
            gdSource.DataBind();
            Session["SeesionName"] = dt;
        }
        else
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("SNO", typeof(int));
            dt.Columns.Add("EmpId", typeof(int));
            DataRow row = dt.NewRow();
            row["EmpId"] = empId;
            row["SNO"] = 1;
            dt.Rows.Add(row);
            gdSource.DataSource = dt;
            gdSource.DataBind();
            Session["SeesionName"] = dt;
        }

答案 1 :(得分:1)

在web.config中将会话超时设置为30分钟

<configuration>
<system.web>
  <sessionState mode="InProc"
                cookieless="false"
                timeout="30"/>
  </sessionState>
</system.web>
</configuration>

在您的申请中

if(Session["YourDataSet"] == null)
    Session["YourDataSet"] = GetDataSetFromSomeWhere();

YourGridView.DataSource = (DataSet)Session["YourDataSet"];
YourGridView.DataBind();