【发布时间】: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