我在将数据写入类字段时遇到问题。我声明了字段“ rcbSelection”,然后在Page_load()中对其进行了初始化。然后,我尝试在一些组合框SelectionChangedEvents中向其中添加数据。
public partial class AddToProcurementPlan : Page
{
Dictionary<string, int?> rcbSelection;
protected void Page_Load(object sender, EventArgs e)
{
try
{
rcbSelection = new Dictionary<string, int?>();
/*other code*/
}
}
protected void rcb_FinancingBudget_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
RadComboBox rcb = (RadComboBox)sender;
if (rcbSelection.ContainsKey("financingbudget"))
rcbSelection["financingbudget"] = int.Parse(rcb.SelectedValue);
else
rcbSelection.Add("financingbudget", int.Parse(rcb.SelectedValue));
}
protected void rcb_BudgetName_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
RadComboBox rcb = (RadComboBox)sender;
if (rcbSelection.ContainsKey("budget"))
rcbSelection["budget"] = int.Parse(rcb.SelectedValue);
else
rcbSelection.Add("budget", int.Parse(rcb.SelectedValue));
}
protected void rcb_FondName_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
RadComboBox rcb = (RadComboBox)sender;
if (rcbSelection.ContainsKey("fond"))
rcbSelection["fond"] = int.Parse(rcb.SelectedValue);
else
rcbSelection.Add("fond", int.Parse(rcb.SelectedValue));
}
protected void rcb_Kfkr_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
RadComboBox rcb = (RadComboBox)sender;
if (rcbSelection.ContainsKey("kfk"))
rcbSelection["kfk"] = int.Parse(rcb.SelectedValue);
else
rcbSelection.Add("kfk", int.Parse(rcb.SelectedValue));
}
protected void rcb_Kekr_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
RadComboBox rcb = (RadComboBox)sender;
if (rcbSelection.ContainsKey("kekr"))
rcbSelection["kekr"] = int.Parse(rcb.SelectedValue);
else
rcbSelection.Add("kekr", int.Parse(rcb.SelectedValue));
}
protected void rcb_BudgetExec_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
RadComboBox rcb = (RadComboBox)sender;
if (rcbSelection.ContainsKey("budgetexec"))
rcbSelection["budgetexec"] = int.Parse(rcb.SelectedValue);
else
rcbSelection.Add("budgetexec", int.Parse(rcb.SelectedValue));
}
protected void rcb_SectionCode_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
RadComboBox rcb = (RadComboBox)sender;
if (rcbSelection.ContainsKey("section"))
rcbSelection["section"] = int.Parse(rcb.SelectedValue);
else
rcbSelection.Add("section", int.Parse(rcb.SelectedValue));
}
protected void rcb_TargetArticle_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
RadComboBox rcb = (RadComboBox)sender;
if (rcbSelection.ContainsKey("targetarticle"))
rcbSelection["targetarticle"] = int.Parse(rcb.SelectedValue);
else
rcbSelection.Add("targetarticle", int.Parse(rcb.SelectedValue));
}
}
但是当我在上一个事件上设置停止点时,我可以看到rcbSelection为空。我做错了什么?请帮助我。
答案 0 :(得分:1)
我怀疑您的rcbSelection字典在您的SelectedIndexChanged事件的回发之后将被重置,因为该字典没有持久保存在诸如ViewState之类的内存中。
考虑将rcbSelection词典持久保存为...
private Dictionary<string, int?> rcbSelection
{
get
{
if(this.ViewState["rcbSelection"] != null)
{
return (Dictionary<string, int?>)this.ViewState["rcbSelection"];
}
return new Dictionary<string, int?>();
}
set
{
this.ViewState["rcbSelection"] = value;
}
}
在page_load事件中,如果您不希望在每次回发后重设字典,请考虑从page_load函数中删除rcbSelection = new Dictionary<string, int?>();
或通过检查它是否是第一页加载来仅初始化一次... < / p>
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
rcbSelection = new Dictionary<string, int?>();
}
// other postback code here ...
}
希望有帮助。