如何在GridView中作为会话变量传递和检索数据

时间:2019-06-13 10:07:39

标签: c# asp.net linq session gridview

我数据库中有一个投诉表,登录后在网格视图中显示。当多个用户同时登录时,我已经创建了一个用户名会话。简单标签会话有效,但是网格视图显示了最近的用户活动(或投诉)表示网格视图在整个会话中均不起作用...我已通过Linq查询绑定了网格视图,请告诉我如何在网格视图上创建会话?

//此代码有效//

  

Login.aspx

       Session["txtname"] = txtname.Text;
       Session["cust_Dept"] = rec.cust_Dept;
  

Admin.aspx

if (!IsPostBack)
            {
                Response.Write("<br/>");
                Response.Write("<b>Welcome : </b>  " + Session["txtname"].ToString());

*// This code is working just showing correct name of userLogin  //*

//现在这是我通过其绑定数据的查询。// //此代码工作简单,但不适用于基于会话的会话///

var test10 = (from u in dbContext.ComplaintComments
              join b in dbContext.Complaints on u.comp_Id equals b.comp_Id
              join a in dbContext.Customers on u.cust_Id equals a.cust_Id

              where a.cust_Id == Global.cust_Id
              orderby u.cc_Timestamp descending 
              select u ).ToList();

 ComplaintsGV.DataSource = test10;
 ComplaintsGV.DataBind();

在网格视图中添加会话时不显示任何错误。就像屏幕上显示的上一个网格视图一样。

2 个答案:

答案 0 :(得分:0)

请按照以下步骤操作:

  1. Login.aspx中添加以下行:

    Session["cust_Id"] = rec.cust_Id;

  2. 在您的linq查询中,使用Session["cust_Id"]而不是Global.cust_Id(我假设客户ID是整数类型-如果不是,则不需要转换):

    // before the query, assign the customer id to a variable
    var cust_Id = Convert.ToInt32(Session["cust_Id"]);
    
    // the query becomes:
    var test10 = (from u in dbContext.ComplaintComments
              join b in dbContext.Complaints on u.comp_Id equals b.comp_Id
              join a in dbContext.Customers on u.cust_Id equals a.cust_Id
    
              where a.cust_Id == cust_Id
              orderby u.cc_Timestamp descending 
              select u ).ToList();
    

答案 1 :(得分:0)

第1页

 protected void Button1_Click(object sender, EventArgs e)
        {
            var rec = (from r in dc.Customers

                       where r.cust_Login == txtname.Text.Trim() && r.cust_Pwd == txtPass.Text.Trim()
                       select r).SingleOrDefault();


            var depart = (from de in dc.Departments
                          where de.dept_Id == rec.cust_Dept
                          select de).Single();

            if (rec != null && rec.cust_Agent == true)

            {

                Session["txtname"] = txtname.Text;
                Session["txtDepartment"] = depart.dept_Id;
                **Session["cid"] = rec.cust_Id;**

                Response.Redirect("response.aspx");
            }

            else {
                Response.Write("invalid");
            }
        }

第2页

 Response.Write("<b>Welcome : </b>  " +Session["txtname"].ToString());
            Response.Write("<br/><b>Department : </b>  " + Session["txtDepartment"].ToString());
            Response.Write("<br/>Customer ID :" +Session["cid"].ToString());





protected void Button1_Click(object sender, EventArgs e)
        {



            Session["cid"].ToString();
            var customerID = (Session["cid"].ToString());




            {
                var test11 = (from u in dc.ComplaintComments

                              join b in dc.Complaints on u.comp_Id equals b.comp_Id
                              join g in dc.ComplaintPriorities on u.cp_id equals g.cp_Id
                              join m in dc.ComplaintStatus on u.cs_Id equals m.cs_Id
                              join t in dc.ComplaintTypes on b.comp_Type equals t.ct_Id
                              join a in dc.Customers on u.cust_Id equals a.cust_Id
                              join x in dc.Departments on a.cust_Dept equals x.dept_Id

                              where a.cust_Id == Convert.ToInt32(Session["cid"].ToString())
                              orderby u.comp_Id ascending
                              select new

                              {
                                  b.comp_Id,
                                  Ticket_Date = b.comp_Date_time,
                                  Issue_Type = t.ct_Name,
                                  Last_Modification = u.cc_Timestamp,
                                  Assigned_To = a.cust_FirstName,
                                  Priority = g.cp_Desc,
                                  Status = m.cs_Name,
                                  Comments = u.cc_Comments,
                                  x.dept_Name
                              }

                                      ).GroupBy(item => item.comp_Id)

                                     .Select(item => item.ToList().First());

                GVTesting.DataSource = test11;
                GVTesting.DataBind();
            }