Demo Application

   

I have created a demonstration application that shows how to examine the contents of the session and application variables, and how to add session and application variables . Four pages perform the following:

  • View the active session variables

  • View the active application variables

  • Create and add a session variable

  • Create and add an application variable

A main screen enables users to select which of these four pages they want to go to, and this can be seen in Figure 17.1.

Figure 17.1. The main menu for the State Management Demo application.

graphics/17fig01.gif

Viewing the Session Variables

The ShowSession.aspx file is where users will go when they want to examine all the current session variables. In the C# code behind this page I used the PageLoad() method to put all the session variables and their contents into a label. You can see the exact code that I used in the following:

 private void Page_Load(object sender, System.EventArgs e)  {      foreach( string strKeyName in Session.Keys )      {          Label1.Text += ( "Key: " + strKeyName + ", Value: " +              Convert.ToString( Session[strKeyName] ) + "<br>\r\n" );      }  } 

You can see this page executing in Figure 17.2.

Figure 17.2. This screen shows the session variables that exist for the current session.

graphics/17fig02.gif

Viewing the Application Variables

The code to retrieve the application variable keys and their contents is almost identical to the code that was used to retrieve session variable information. The only difference is that rather than access session variables, it accesses application variables. The following code shows the PageLoad() method of the ViewApplication.ASPX file:

 private void Page_Load(object sender, System.EventArgs e)  {      foreach( string strKeyName in Application.Keys )      {          Label1.Text += ( "Key: " + strKeyName + ", Value: " +              Convert.ToString( Application[strKeyName] ) + "<br>\r\n" );      }  } 

You can see this page running in Figure 17.3.

Figure 17.3. This page shows the application variables that exist.

graphics/17fig03.gif

Adding a Session Variable

It is easy to add a session variable. The page that does this is named SetSession.ASPX. It has two editable text fields into which users can type a key name , and then some string data that will be placed into the session variable. The code that sets the session variable is simple and can be seen here:

 private void Button1_Click(object sender, System.EventArgs e)  {      if( TextBox1.Text.Length > 0 &&          TextBox2.Text.Length > 0 )      {          Session[TextBox1.Text] = TextBox2.Text;          Response.Redirect( "ViewSession.aspx" );      }  } 

This page can be seen executing in Figure 17.4.

Figure 17.4. You can add any session variables in this screen.

graphics/17fig04.gif

Setting Application Variables

Setting application variables is as easy as setting session variables. The code is almost identical to the code that sets session variables, and is as follows :

 private void Button1_Click(object sender, System.EventArgs e)  {      if( TextBox1.Text.Length > 0 &&          TextBox2.Text.Length > 0 )      {          Application[TextBox1.Text] = TextBox2.Text;          Response.Redirect( "ViewApplication.aspx" );      }  } 

You can see the page that enables users to set application variables running in Figure 17.5.

Figure 17.5. This screen enables you to add application variables.

graphics/17fig05.gif

   


Special Edition Using ASP. NET
Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net