State Management and Web Application
 
An Example of State Management
 
1. Open the global.asax (add if not available).

2. Copy the code written below or write it .
 
Sample Code for Global.asax
 
<%@ Application Language="C#" %>

<script runat="server">

void Application_Start(object sender, EventArgs e)
{

// An Application variable to maintain the state at application level.

Application["appstate"] = "Application String";

// Code that runs on application startup.

}

void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}

void Session_Start(object sender, EventArgs e)
{

// An Session variable to maintain the state at session level.

Session["mysession"] = "Session String";
// Code that runs when a new session is started
}

void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
}
</script>
 
Sample Code for Default.aspx Web Page.
 
using System;
using System.Data; using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(Object sender, EventArgs e)
{
// The ViewState variable to maintain the state at Page level.

ViewState["myviewstate"] = "ViewState String";

//To write the text to web page.

Response.Write( "This is Session Value: " +
Session["mysession"].ToString() + "<br>
");

}

protected void mybutton_Click(object sender, EventArgs e)
{
//To move to different web page or url.

Response.Redirect("NewWebForm.aspx?Name=ebiz&Open=yes");

}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("This is ViewState Value: " + ViewState["myviewstate"].ToString());
}
}

using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(Object sender, EventArgs e)
{
// The ViewState variable to maintain the state at Page level.

ViewState["myviewstate"] = "ViewState String";

//To write the text to web page.

Response.Write( "This is Session Value: " + Session["mysession"].ToString() + "<br>
");

}

protected void mybutton_Click(object sender, EventArgs e)
{
//To move to different web page or url.

Response.Redirect("NewWebForm.aspx?Name=ebiz&Open=yes");

}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("This is ViewState Value: " + ViewState["myviewstate"].ToString());

}
}
 
Desgin View of Default Web Page.
 
 
Sample Code for NewWebForm.aspx Web Page.
 
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;

public partial class NewWebForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string name;
//To get value of Query String which was passed in default.aspx page.

name = "Name:" + Request.QueryString["Name"].ToString();
Response.Write(name + "<br>
<br>");

//To get value of Session Variable which was assigned in default.aspx page.

Response.Write( "This is Session Value: " + Session["mysession"].ToString());

//To get value of ViewState Variable which was assigned in default.aspx page.