【问题标题】:How to pass & retrieve data in gridview as session variable如何将gridview中的数据作为会话变量传递和检索
【发布时间】:2019-10-27 21:49:02
【问题描述】:

我在数据库中有一个投诉表,登录后在网格视图中显示当多个用户同时登录时,我已经创建了一个用户名会话,简单标签会话正在工作,但网格视图显示了最近的用户活动(或投诉)意味着网格视图无法通过会话工作...我已通过 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();

在网格视图中添加会话时不显示任何错误。它的工作方式与屏幕上先前的网格视图显示一样。

【问题讨论】:

  • 您所说的网格视图中的会话是什么意思?
  • @Rahul 它工作得很好,我只想在我使用“Ahsan”帐户登录时如何使用会话并控制我的网格视图,它完美地显示我的会话名称但是当新用户登录时假设“Rahul”登录它通过会话显示您的姓名,但网格视图会自动更改数据并显示最近登录客户的数据。我希望当两个客户同时登录时网格视图显示他们自己的数据。

标签: c# asp.net linq session gridview


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    按照以下步骤操作:

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

    【讨论】:

    • 我对您的答案进行了一些编辑。否则,LINQ 将尝试映射 Session。
    • 非常感谢你,但我已经改变了它,你的逻辑很完美
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多