Asp.net 如何在viewstate中保存字典值?

Asp.net 如何在viewstate中保存字典值?,asp.net,viewstate,datapersistance,Asp.net,Viewstate,Datapersistance,我有一个问题和答案页面,当一些问题被隐藏,而另一些问题根据先前的答案而可见时。最后我需要所有的答案。 我正试着用字典来解释这件事。我在每个控件的更改事件上添加带有键的答案。但是当我移动到下一节时,没有一个答案被保存,因为我隐藏了前面的部分 我正在尝试将其保存在viewstate中并添加值,以便我的值在字典中保持不变。。 有什么想法吗 代码如下: Dictionary<String, String> Answers; protected void Page_Load(

我有一个问题和答案页面,当一些问题被隐藏,而另一些问题根据先前的答案而可见时。最后我需要所有的答案。 我正试着用字典来解释这件事。我在每个控件的更改事件上添加带有键的答案。但是当我移动到下一节时,没有一个答案被保存,因为我隐藏了前面的部分

我正在尝试将其保存在viewstate中并添加值,以便我的值在字典中保持不变。。 有什么想法吗

代码如下:

    Dictionary<String, String> Answers;

    protected void Page_Load(object sender, EventArgs e)
    {
        Answers = (Dictionary<String, String>)ViewState["Answers"];
        ViewState["Answers"] = Answers;

    }

    protected void rdb_study_popul_SelectedIndexChanged(object sender, EventArgs e)
    {

        ViewState["StudySamplepopulation"] = rdb_study_popul.SelectedValue.ToString();
        Answers.Add("StudyPopulation", ViewState["StudySamplepopulation"].ToString());


    }

您不需要在页面加载中更新
ViewState
,因为它尚未更改,因此请删除
页面加载中
ViewState
的分配,改为执行以下操作:

protected void Page_Load(object sender, EventArgs e)
{
    // Only read the value from ViewState, do not update it here
    Answers = (Dictionary<String, String>)ViewState["Answers"];
}
更新:

您需要检查
ViewState
是否为空,如下所示:

protected void rdb_study_popul_SelectedIndexChanged(object sender, EventArgs e) 
{
    ViewState["StudySamplepopulation"] = rdb_study_popul.SelectedValue.ToString();
    Answers.Add("StudyPopulation", ViewState["StudySamplepopulation"].ToString());

    // Now that you are done altering the Answers, save the updated value to ViewState
    ViewState["Answers"] = Answers;
}
Dictionary<String, String> Answers = new Dictionary<String, String>();

protected void Page_Load(object sender, EventArgs e)
{
    // Is there anything in ViewState?
    if (ViewState["Answers"] != null)
    {
        // Yes, but only read the value from ViewState, do not update it here
        Answers = (Dictionary<String, String>)ViewState["Answers"];
    }
}
Dictionary Answers=newdictionary();
受保护的无效页面加载(对象发送方、事件参数e)
{
//ViewState中有什么东西吗?
if(ViewState[“Answers”!=null)
{
//是,但仅从ViewState读取值,不在此处更新
答案=(字典)视图状态[“答案”];
}
}

注意:我更改了
答案
字典的声明,使其也包含实例化,这样它就不会为空。

您不需要在页面加载中更新
视图状态
,因为它尚未更改,所以请删除
页面加载
中的
视图状态
的赋值,改为:

protected void Page_Load(object sender, EventArgs e)
{
    // Only read the value from ViewState, do not update it here
    Answers = (Dictionary<String, String>)ViewState["Answers"];
}
更新:

您需要检查
ViewState
是否为空,如下所示:

protected void rdb_study_popul_SelectedIndexChanged(object sender, EventArgs e) 
{
    ViewState["StudySamplepopulation"] = rdb_study_popul.SelectedValue.ToString();
    Answers.Add("StudyPopulation", ViewState["StudySamplepopulation"].ToString());

    // Now that you are done altering the Answers, save the updated value to ViewState
    ViewState["Answers"] = Answers;
}
Dictionary<String, String> Answers = new Dictionary<String, String>();

protected void Page_Load(object sender, EventArgs e)
{
    // Is there anything in ViewState?
    if (ViewState["Answers"] != null)
    {
        // Yes, but only read the value from ViewState, do not update it here
        Answers = (Dictionary<String, String>)ViewState["Answers"];
    }
}
Dictionary Answers=newdictionary();
受保护的无效页面加载(对象发送方、事件参数e)
{
//ViewState中有什么东西吗?
if(ViewState[“Answers”!=null)
{
//是,但仅从ViewState读取值,不在此处更新
答案=(字典)视图状态[“答案”];
}
}

注意:我更改了
答案
字典的声明,使其也包含实例化,这样它就不会为null。

我得到null reference errorObject reference未设置为对象的实例。公共部分类{Dictionary Answers;protected void Page_Load(object sender,EventArgs e){Answers=(Dictionary)ViewState[“Answers”];}protected void rdb_studysubj_selected dexchanged(object sender,EventArgs e{ViewState[“StudySubjectAnimal”]=rdb_studysubysubj.SelectedValue.ToString();studysub_popul.Visible=true;Answers.Add(“StudySubjectAnimal”,ViewState[“StudySubjectAnimal”].ToString();ViewState[“Answers”]=Answers;}字典只保存了一个答案。我在前进时会隐藏和显示控件并将其保存在字典中,但当我最终检索到答案时,它只保存了最后一个答案,不会显示所有答案这是我最后如何阅读字典\u容量\u提交\u单击(object sender,EventArgs e){Answers=(Dictionary)ViewState[“Answers”];foreach(Answers中的KeyValuePair项){test.Text+=“KeyName”+::“+item.Key+“Value”+::“+item.Value+”
“;}是否有多个控件(如
rdb_study_populi
)用于处理选定的索引更改事件?是的,有多个控件,在我进入下一个问题时,每个控件都被隐藏。其中一些控件是复选框列表、下拉列表、radiobuttonlist。我正在尝试获取所有这些控件的值并将其保存在dictionary带有一个键,我需要所有的答案来进行更多的分析我得到的是空引用errorObject引用没有设置为对象的实例。公共部分类{dictionary answers;protected void Page_Load(object sender,EventArgs e){answers=(dictionary)ViewState[“answers”;}受保护的无效rdb_studysubj_SelectedIndexChanged(对象发送者,事件参数e{ViewState[“StudySubjectAnimal”]=rdb_studysubj.SelectedValue.ToString();studysub_populi.Visible=true;Answers.Add(“StudySubjectAnimal”,ViewState[“StudySubjectAnimal”].ToString());ViewState[“Answers”]=Answers;}字典只保存了一个答案。我在前进时会隐藏和显示控件并将其保存在字典中,但当我最终检索到答案时,它只保存了最后一个答案,不会显示所有答案这是我最后如何阅读字典\u容量\u提交\u单击(object sender,EventArgs e){Answers=(Dictionary)ViewState[“Answers”];foreach(Answers中的KeyValuePair项){test.Text+=“KeyName”+::“+item.Key+“Value”+::“+item.Value+”
“;}是否有多个控件(如
rdb_study_populi
)用于处理选定的索引更改事件?是的,有多个控件,在我进入下一个问题时,每个控件都被隐藏。其中一些控件是复选框列表、下拉列表、radiobuttonlist。我正在尝试获取所有这些控件的值并将其保存在带钥匙的字典,我最后需要所有的答案以便进一步分析