locked
State between pages RRS feed

  • Question

  • User-2013558545 posted

    Hi,

    What is the way to go if I have the following situation:

    Main Page 

       Sub Page 1

       Sub Page 2 

    Main Page needs to grab information from Sub Page 1 and Sub Page 2, but I dont want to lose the information that the user has entered on Main Page while I am on the Sub Pages.

    In a windows environment I would simply call a dialog window and return the information I want back to the Main Page. I have dialog windows (telerick controls) showing at the moment but it is getting pretty messy as the Sub Page could call another page and another.

    Is there a standard practice for such a situation? Do I use session variables or some other way to do this?

    Thank you.

    Wednesday, August 10, 2011 8:56 PM

Answers

  • User-481631678 posted

    I think that you should use session to save data if your site is an normal small/medium size appliaction. First create a class which contain all the fields of your first page, e.g. If I want to take input from user about Category, Sub Catagory and some detial items, I can create a class like this:-

    public Class MyEntity
    {
    
    string Cartegory {get;set;}
    string SubCategory {get;set;}
    
    List<MyItem> Items {get;set;}
    }
    
    public Class MyItem
    {
    string ItemName {get;set;}
    }


    Now in your first page, on a button click where you are redirecting to second page, you can fill the entity and save it in session like this:-

    MyEntity entity = new MyEntity();
    
    entity.Category=TextBox1.Text;
    entity.SubCategory = TextBox2.Text;
    entity.Items= new List<MyItem>();
    
    for(int i=0;i<10;i++)
    {
    Item item = new Item();
    item.ItemName = i.ToString();
    }
    
    Session["MyEntity"] = entity;


    Now in the page where you want to display information, you can get entity from Session and display results to textbox,label etc like this:-

    if(Session["MyEntity"]!=null)
    {
    MyEntity entity = (MyEntity)Session["MyEntity"];
    
    TextBox1.Text=entity.Category;
    TextBox2.Text=entity.SubCategory;
    }


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 11, 2011 5:37 AM

All replies

  • User-573138384 posted

    Do I use session variables or some other way to do this?
    Absolutely. In web environment, you will loose info every time the page is submitted. Even the same page is submitted multiple times. So ASP.NET comes up with good state management techniques. It asks you what do you want. Do you want in the same page? Do you want in all pages? Do you want for all users? etc...

    In your scenario, Session works fine. I hope the data is not too heavy.

    Wednesday, August 10, 2011 9:06 PM
  • User692200542 posted

    If you want to use it in the whole application, I would ask you to use application state ... It is as same how we do in windows ;) 

    Thursday, August 11, 2011 1:33 AM
  • User-481631678 posted

    I think that you should use session to save data if your site is an normal small/medium size appliaction. First create a class which contain all the fields of your first page, e.g. If I want to take input from user about Category, Sub Catagory and some detial items, I can create a class like this:-

    public Class MyEntity
    {
    
    string Cartegory {get;set;}
    string SubCategory {get;set;}
    
    List<MyItem> Items {get;set;}
    }
    
    public Class MyItem
    {
    string ItemName {get;set;}
    }


    Now in your first page, on a button click where you are redirecting to second page, you can fill the entity and save it in session like this:-

    MyEntity entity = new MyEntity();
    
    entity.Category=TextBox1.Text;
    entity.SubCategory = TextBox2.Text;
    entity.Items= new List<MyItem>();
    
    for(int i=0;i<10;i++)
    {
    Item item = new Item();
    item.ItemName = i.ToString();
    }
    
    Session["MyEntity"] = entity;


    Now in the page where you want to display information, you can get entity from Session and display results to textbox,label etc like this:-

    if(Session["MyEntity"]!=null)
    {
    MyEntity entity = (MyEntity)Session["MyEntity"];
    
    TextBox1.Text=entity.Category;
    TextBox2.Text=entity.SubCategory;
    }


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 11, 2011 5:37 AM