组合框 SelectionChangedEvent 不写入 class 字段

Combobox SelectionChangedEvent doesn't write to class field

我在向 class 字段写入数据时遇到问题。我声明字段 "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 是空的。我做错了什么?请帮助我。

我怀疑你的 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?>(); 或通过检查是否仅初始化一次这是第一页加载者...

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                rcbSelection = new Dictionary<string, int?>();
            }

            // other postback code here ... 
        }

希望对您有所帮助。