【发布时间】:2013-11-11 17:41:21
【问题描述】:
我的 Web 应用程序中发生了一些奇怪的事情。我有一个静态字典,用于保存包含两个变量的简单对象的集合:
static Dictionary<string, linkButtonObject> linkButtonDictonary = new Dictionary<string, linkButtonObject>();
我有一个带有链接按钮的网格视图,每个按钮的数据都与其在字典中的 button.UniqueId 相关联:
protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
LinkButton btn = (LinkButton)e.Row.FindControl("taskLinkButton");
linkButtonObject currentRow = new linkButtonObject();
currentRow.storyNumber = e.Row.Cells[3].Text;
currentRow.TaskName = e.Row.Cells[5].Text;
linkButtonDictonary.Add(btn.UniqueID, currentRow);
}
然后,当单击链接按钮时,我使用 UniqueId 在字典中查找值,在 SQL 查询中使用它们并使用检索到的数据填充网格视图、标签并显示弹出窗口:
protected void taskLinkButton_Click(object sender, EventArgs e)
{
//create linkbutton object from sender
LinkButton btn = (LinkButton)sender;
//get a list of data relevant to column
string[] infoData = getInfoData(linkButtonDictonary[btn.UniqueID].storyNumber,
linkButtonDictonary[btn.UniqueID].TaskName);
//assign content of list to labels and gridview
productDatabaseLabel.Text = infoData[0];
storyNumberDatabaseLabel.Text = infoData[1];
taskDatabaseLabel.Text = infoData[2];
pointPersonDatabaseLabel.Text = infoData[3];
SqlDataSource6.SelectParameters["storynumber"].DefaultValue = linkButtonDictonary[btn.UniqueID].storyNumber;
SqlDataSource6.SelectParameters["tasktitle"].DefaultValue = linkButtonDictonary[btn.UniqueID].TaskName;
infoGridView.DataBind();
//show popup
MPE.Show();
}
这一切都很好,我可以单击任何链接按钮,它们会正确创建和填充弹出窗口并显示它。
我的问题是,如果我让页面闲置几分钟,然后单击链接按钮,我会收到错误消息:
我做错了什么,我该如何解决?
【问题讨论】:
-
为什么字典被声明为静态的?如果您删除静态修饰符,是否会出现此问题?
-
如果我删除静态,我每次都会得到异常。
-
是什么异常? KeyNotFound 还是 null?
-
KeyNotFound 异常。
标签: c# asp.net dictionary