1

I have a questions and answers page when some questions gets hidden and some gets visible depending upon the predecessor answers. at the end i need all the answers. I am trying to use dictionary for this. I add answers with a key on a changed event of each control. but none of the answers are saved when i move to next section as i am hiding the previous sections.

I am trying to save it in a viewstate and add the values so that my values persists in the dictionary.. Any ideas?

Here is the code:

    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());


    }

Right now, Answers will be empty if I move to next section.

Any help would be greatly appreciated.


i am getting null reference error Object reference not set to an instance of an object.

Public partial class { Dictionary Answers;

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

    }
  protected void rdb_studysubj_SelectedIndexChanged(object sender, EventArgs e)
    {
           ViewState["StudySubjectAnimal"] = rdb_studysubj.SelectedValue.ToString();
            studysub_popul.Visible = true;                
            Answers.Add("StudySubjectAnimal", ViewState["StudySubjectAnimal"].ToString());
            ViewState["Answers"] = Answers;
        }
Bohemian
  • 412,405
  • 93
  • 575
  • 722
user2664298
  • 175
  • 1
  • 7
  • 22

1 Answers1

1

You do not need to update the ViewState in page load, because it has not changed yet, so remove the assignment of ViewState in your Page_Load and do this instead:

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"];
}

Now in you event handler, when you are done adding to the Answers, then you should update your ViewState value, like this:

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;
}

UPDATE:

You need to check if ViewState is null or not, like this:

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"];
    }
}

Note: I changed the declaration of the Answers dictionary to also include instantiating it, so that it will not be null.

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
  • I am getting null reference errorObject reference not set to an instance of an object. public partial class { Dictionary Answers;protected void Page_Load(object sender, EventArgs e) { Answers = ( Dictionary)ViewState["Answers"]; } protected void rdb_studysubj_SelectedIndexChanged(object sender, EventArgs e{ViewState["StudySubjectAnimal"]=rdb_studysubj.SelectedValue.ToString();studysub_popul.Visible=true;Answers.Add("StudySubjectAnimal",ViewState["StudySubjectAnimal"].ToString());ViewState["Answers"] = Answers;} – user2664298 Aug 12 '13 at 17:51
  • the dictionary is saving only one answer. I hide and show the controls as i move forward saving them in the dictionary but when i retrieve the answers at last it only saves the last one it doesn't displays all the answers – user2664298 Aug 13 '13 at 13:58
  • this is how i reading the dictionary at last protected void Laboratory_Capacity_Submit_Click(object sender, EventArgs e) { Answers = (Dictionary)ViewState["Answers"]; foreach (KeyValuePair item in Answers) { test.Text += "KeyName" + "::" + item.Key + "Value" + "::" + item.Value + "
    "; } }
    – user2664298 Aug 13 '13 at 14:01
  • Is there more than one control like `rdb_study_popul` that you are handling a selected index changed event for? – Karl Anderson Aug 13 '13 at 14:09
  • yes there are multiple controls like that and each of them gets hidden as i go to next question. some of them are checkboxlist, dropdownlist,radiobuttonlist. i am trying to get the values of all these controls and save it in a dictionary with a key, i need the all the answers at last for more analaysis – user2664298 Aug 13 '13 at 14:14
  • it is same code file. i am actually writing a user control which has all these controls and their changed event handlers. every time an event is fired i am saving the answers and moving forward to next question. this is s single user controls with all the controls having autopostback equals true and i am using ajax for the partial postback. I am trying to save all the answers one by one in a dictionary as a collection with a key so that it will eb helppful for me to retireve at the last – user2664298 Aug 13 '13 at 14:24