views:

38

answers:

4

Hi all,

I've build a class like this:

private class TestResults
{
    public bool IsAdmitted { get; set; } 
    public bool IsDuplicate { get; set; } 
    public bool IsVerified { get; set; } 
}

The values of this class are set at postback by clicking a radiobutton list. Problem however is I don't know how to save this values across multiple postbacks. I thought of using viewstate but I'm not quite sure how to do it with this class.

Maybe I'm missing some important thing here.

Hope someone can point me in the right direction

thanks for your time! Kind regards, Mark

A: 

You can use

Session.Add("TestResults", Your_Test_Result_Object)

Session Object Explanation

Kevin
+1  A: 

try using the Session cache instead

 var testResults = new TestResults();
 //set values
 Session["TestResults"] = testResults;

Retrieving them later on:

 var testResults = Session["TestResults"] as TestResults;
 if (testResults != null)
 {
      //use it
 }
Daniel Dyson
Thanks Daniel excactly what I was looking for!
Mark
You are welcome Mark. Would you mind marking the answer as correct?
Daniel Dyson
A: 

Just sticking this class in viewstate is pretty simple:

ViewState["SomeUniqueKey"] = myTestResults;

var testResults = (TestResults)ViewState["SomeUniqueKey"];

Your class will need to be marked with the [Serializable] attribute though.

Josh
A: 

If you don't need this value on other pages or in other places throughout the app, you can use viewstate.

Use the Page.ViewState object bag to store it:

public partial class Page1 : Page {
    protected void button1_click(object sender, EventArgs e) {
        ViewState["myObject"] = testResultsObject;
    }
}

You could also wrap the access to it in a property on the page:

public partial class Page1 : Page {
    public TestResults TestResults { 
        get{ return ViewState["TestResults"] as TestResults; }
        set{ ViewState["TestResults"] = value; }
    }   
}
Zachary Yates