【问题标题】:Maintaining session object in webforms在 Web 表单中维护会话对象
【发布时间】:2015-02-16 22:15:52
【问题描述】:

我试图做的是保留一个存储 Dictionary 的会话对象。

我需要这本字典来保存与 asp:Gridview 匹配的运行列表。

每次页面加载时,我都会检查字典并突出显示 Gridview 中的所有匹配项。

但是,每次 Page_Load 发生时,Session["Rolls"] 都会显示为 null。现在,我还在 buttonClick 事件中突出显示匹配的条目,并且字典被保留,直到我启动不同的事件(如另一个按钮单击或 GridView_RowEditing/GridView_RowUpdating)。是否有任何我不关注的 Session 变量主体?

Google Chrome 控制台也会在每个操作中识别 Session,但在 Debug 中,每个 Page_Load 都会产生一个空 Session["Rolls"]。

这是我的 Page_Load 代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["Rolls"] = new Dictionary<string, int>();
        }
        else
        {
            WoSource.SelectCommand =
            "SELECT WorkOrderNo, RollNumber, ModelNumber, QtyGood, QtyRun FROM RFID_Inventory WHERE WorkOrderNo= '" + woText.Text + "'";

            var currentDict = (Dictionary<string, int>) Session["Rolls"];
            if (currentDict == null)
            {

            }
            else
            {
                foreach (var entry in currentDict)
                {
                    foreach (GridViewRow row in GridView1.Rows)
                    {
                        var dataKey = GridView1.DataKeys[row.RowIndex];
                        if (dataKey != null && (dataKey["RollNumber"].ToString() == entry.Key && entry.Value == 0))
                        {
                            row.BackColor = System.Drawing.Color.Red;
                            break;
                        }
                        if (dataKey != null && (dataKey["RollNumber"].ToString() == entry.Key && entry.Value == 1))
                        {
                            row.BackColor = System.Drawing.Color.Green;
                            break;
                        }
                    }
                }   
            }
        }
    }

编辑:发现 RowEditing/RowUpdating 事件没有保留我正在做的 GridView Backcolor 突出显示。

有什么我可以添加到这些事件调用中的吗?

这是我的 RowEditing 活动:

protected void GridView1_RowEditing(object sender, EventArgs e)
    {
        var currentDict = (Dictionary<string, int>)Session["Rolls"];
        if (currentDict == null)
        {

        }
        else
        {
            foreach (var entry in currentDict)
            {
                foreach (GridViewRow row in GridView1.Rows)
                {
                    var dataKey = GridView1.DataKeys[row.RowIndex];
                    if (dataKey != null && (dataKey["RollNumber"].ToString() == entry.Key && entry.Value == 0))
                    {
                        row.BackColor = System.Drawing.Color.Red;
                        break;
                    }
                    if (dataKey != null && (dataKey["RollNumber"].ToString() == entry.Key && entry.Value == 1))
                    {
                        row.BackColor = System.Drawing.Color.Green;
                        break;
                    }
                }
            }
        }
    }

EDIT2:我的问题已解决。我的会话没有回来为空。我实际上需要将我的 GridView 突出显示方法添加到 GridView_RowDataBound 事件。原来我只是想在错误的时间突出显示网格视图。我将 Kishore 的答案标记为正确,以便用户可以看到我的回复。感谢大家的帮助。

【问题讨论】:

  • 小心你的sql查询中的sql注入
  • 在本地网络应用程序上需要担心什么吗?
  • 最佳实践是始终保持安全,您永远不知道会发生什么。如果该代码在另一个 Web 应用程序中重用怎么办?

标签: c# asp.net session gridview


【解决方案1】:

当我查看您的代码时,您正在页面加载时初始化会话变量,因此您丢失了状态。您可以在初始化会话变量之前进行空检查。

【讨论】:

  • 这只是在第一个 Page_Load 上。每个事件都会调用在 Page_Load 中检查的回发。一个人离开页面后,我不需要保留会话。
  • 没关系。在会话变量中填写数据的位置?
  • 好吧,我发现了这个问题。我将我的 GridView 突出显示方法添加到事件 GridView_RowDataBound 中,这解决了我的问题。我会将您的答案标记为正确,以便用户看到这一点。也感谢您的帮助。
【解决方案2】:

嗯,我们将全局对象管理到会话中所做的是将它们封装在属性中。让我们看看这个:

public Dictionary<string, int> Rolls
{
   get 
   {
       if(Session["Rolls"] == null) 
           return new Dictionary<string, int>();
       else
           return (Dictionary<string, int>)Session["Rolls"];
   }

   set
   {
      Session["Rolls"] = value;
   }
}

protected void Page_Load(object sender, EventArgs e)
{
   // Now Accessing Rolls in page will be directly hitting Session.
   // and Get property will give New dictionary object with 0 record instead of null
   // Otherwise it will return Object already caste into dictionary object

   foreach (var entry in Rolls) 
   {

   }

   // We always can assign new Dictionary object in Session by
   Rolls = new Dictionary<string, int>(10); // for example dictionary with 10 items
}

所以我们可以在网页的父类中使用我们的 Get Set 来使其在所有子类上都可以访问。我们也可以对 View state 和 Cache 对象使用类似的方法。

如果您有任何问题,请告诉我。

【讨论】:

    猜你喜欢
    • 2016-03-01
    • 1970-01-01
    • 2023-04-09
    • 2019-03-22
    • 2018-09-05
    • 1970-01-01
    • 1970-01-01
    • 2011-11-08
    • 2013-11-18
    相关资源
    最近更新 更多