Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Would like to know simple examples on Session & application variable in asp.net.
I have created 3 pages, I am navigating from a to b, b to c, & again c to a. I would like to have the session counter to increase by 1. When i return back on pag e 'a'. But the session counter is increasing by 2 instead of 1.

Kindly suggest the desired output.

Thanks in advance..
Posted

1 solution

Here's a quick fix for that.

First, create a class that will handle your SessionCounter;
C#
public class SessionCounter {
   public SessionCounter() {
      Counter = 1;
      LastPage = "A";
   }

   public int Counter { get; set; }
   public string LastPage { get; set; }
}



Then on your webpage insert this on your Page_Load

C#
protected void Page_Load(object sender, EventArgs e) {
            if (!IsPostBack) {
                SessionCounter sc = new SessionCounter();

                if (Session["SessionCounter"] != null) {
                    sc = (SessionCounter)Session["SessionCounter"];

                    //put your page letter here as the last page to trigger the increment 
                    //In your case, its page C
                    if (sc.LastPage == "C") {
                        sc.Counter++;
                    }

                    //put your page letter here (sample is for page A)
                    //update this per page
                    sc.LastPage = "A";
                }

                Session["SessionCounter"] = sc;
            }
        }


Good luck!
 
Share this answer
 
Comments
Sandeep Mewara 20-Apr-11 4:16am    
My 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900