在网络表单中维护会话对象

Maintaining session object in webforms

我想做的是保留一个存储字典的会话对象。

我需要这本字典,以便保留一个 运行 列表,最多匹配 asp:Gridview。

每次加载页面时,我都会检查词典并突出显示网格视图中的所有匹配项。

但是,每次 Page_Load 发生时,Session["Rolls"] 都会显示为空。现在,我还在 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: 我的问题已经解决了。我的会话没有返回 null。我实际上需要将我的 GridView 突出显示方法添加到 GridView_RowDataBound 事件。原来我只是想在错误的时间突出显示 gridview。我将 Kishore 的回答标记为正确,以便用户可以看到我的回复。感谢大家的帮助。

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

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

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
}

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

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