【发布时间】:2017-03-16 09:53:30
【问题描述】:
在我的 asp.net 应用程序中,我正在从文本框中检索数据并尝试创建一个字典对象。
protected void btnAddBarcode_Click(object sender, EventArgs e)
{
try
{
Dictionary<int, string> Barcodes = new Dictionary<int, string>();
int i = Convert.ToInt32(lblItemsScanned.Text);
if (string.IsNullOrEmpty(txtBarcode.Text.Trim()))
{
ShowMessage("Please enter or scan barcode");
return;
}
// Saving the Barcodes at first
else if (i < Convert.ToInt32(hdnTotalItems.Value))
{
i++;
if (i == 1)
{
Barcodes.Add(i, txtBarcode.Text);
lblItemsScanned.Text = i.ToString();
Session["Barcodes"] = Barcodes;
txtBarcode.Text = string.Empty;
i++;
}
}
else {
}
}
第一次数据被保存在字典中,但第二次字典被第一个更新。我想保留它们。
任何形式的帮助都会被优雅地接受。
【问题讨论】:
-
您每次都创建一个新实例。仅当会话中没有一个时才创建一个新的
-
您在方法内将您的字典声明为“本地”变量。您必须将其作为字段放入对象中
标签: c# asp.net dictionary