locked
Create session for a class RRS feed

  • Question

  • User-284642143 posted

    I have a class called Customers which has 2 properties (Name and Surname) - For simplicity i have 2 properties but in reality i have around 10 properties.

    I have a parent form and a child form. I would like to pass the values from the parent to the child and when the child closes to pass the same values back to the parent form so that no data on the parent form is lost that the user entered before going to the child form.

    So i know i can create a session to hold the data 

    Session["Name"] = NameTextBox.Text;

    but i don't want to repeat that for all properties and wondered if there is a way to pass the class into the session which would have access to all values that the user entered?

    Thursday, March 24, 2016 11:30 AM

Answers

  • User475983607 posted

    EssCee

    but i don't want to repeat that for all properties and wondered if there is a way to pass the class into the session which would have access to all values that the user entered?

    Yeah, decorate the class with the serializable attribute and assign the class to a Session object. Then use the following pattern for retrieving values from Session.

    Customer customerInstance  = new customerInstance();
    
    if(Session["Customer"] == null){
    	customerInstance = GetCustomer();
    	Session["Customer"] = customerInstance;
    }
    else 
    {
    	customerInstance = (Customer)Session["Customer"] 
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 24, 2016 12:01 PM

All replies

  • User475983607 posted

    EssCee

    but i don't want to repeat that for all properties and wondered if there is a way to pass the class into the session which would have access to all values that the user entered?

    Yeah, decorate the class with the serializable attribute and assign the class to a Session object. Then use the following pattern for retrieving values from Session.

    Customer customerInstance  = new customerInstance();
    
    if(Session["Customer"] == null){
    	customerInstance = GetCustomer();
    	Session["Customer"] = customerInstance;
    }
    else 
    {
    	customerInstance = (Customer)Session["Customer"] 
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 24, 2016 12:01 PM
  • User753101303 posted

    Hi,

    Or don't you have an id for this Customer? Then you could just pass the id and reload those data. If parent form data are not yet saved I would either force the main page to be completed before entering details or I would save data marking them as "not validated" before proceeding to the detail page.

    Thursday, March 24, 2016 12:15 PM