Asp.net store object class in viewstate (or other ideas)

I created 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 during postback by clicking on the list of radio objects. The problem, however, I do not know how to store these values ​​in multiple postbacks. I was thinking about using viewstate, but I'm not quite sure how to do this with this class.

Perhaps I am missing some important thing here.

Hope someone can point me in the right direction

thank you for your time! Regards, Mark

+3
source share
4 answers

Just sticking to this class in viewstate is pretty simple:

ViewState["SomeUniqueKey"] = myTestResults;

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

However, your class must be marked with an attribute [Serializable].

+6

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

:

 var testResults = Session["TestResults"] as TestResults;
 if (testResults != null)
 {
      //use it
 }
+1

you can use

Session.Add("TestResults", Your_Test_Result_Object)

Session Object Explanation

0
source

If you do not need this value on other pages or in other places in the application, you can use viewstate.

Use the object bag Page.ViewStateto save it:

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

You can also wrap 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; }
    }   
}
0
source

Source: https://habr.com/ru/post/1763800/


All Articles