Sessions:
Sessions can be used to store even complex data for the user just like cookies.



Sessions can be used easily in ASP.NET with the Session object. We will re-use the sessions . Keep in mind though, that sessions will expire after a certain amount of minutes, as configured in the web.config file.
<sessionState timeout="600"/>

The Session context not only can hold values across page requests, it can also first fire events on Session Start and on Session End. These will be very useful in setting up our tracking object and handling notification and reporting.

In Login.aspx


 protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
Session.Clear();          // Clears the Session Data
Session.Abandon();        // Clears the session objects completly
string username=txtusername.Text;
                Session["username"] = username.ToString();
            }
           
        }
        catch
        {

        }
    }
We can access this value in any page where ever we want untill the session will expire and session data will clear by us.


In Home.aspx:


protected void Page_Load(object sender, EventArgs e)
    {
        try
        {           
string username=Session["username"].ToString();
                lbluserName.Text = username;           
           
        }
        catch
        {

        }
    }


Share it

0 comments:

Post a Comment