在webforms中维护会话对象

时间:2015-02-16 13:39:08

标签: c# asp.net session gridview

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

我需要这个词典,以便保持一个与asp:Gridview匹配的运行列表。

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

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

Google Chrome控制台还会在每个操作中识别会话,但在Debug中,每个Page_Load都会显示一个空会话[“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事件中。结果我只是想在错误的时间突出显示gridview。我将Kishore的答案标记为正确,以便用户可以看到我的回复。谢谢大家的帮助。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

那么我们将全局对象管理到会话中的做法是将它们封装在Property中。让我们来看看:

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
}

因此,我们可以在Web页面的Parent类中使用Get Set,使其可以在所有子类上访问。我们也可以对View状态和Cache对象使用类似的方法。

如果您有任何疑问,请与我联系。