Work With Page State

You can use Page State to store information that does not span multiple pages. This is appropriate for persisting data on a single page that is used across multiple requests of the same page.

To place a variable into Page State, use the syntax

ViewState['Name'] = Value. To read the variable out of Page State, use the syntax Variable = ViewState['Name']. ViewState is inherited by the Control class. Page class and all Web controls are derived from the Control class and, therefore, have the ability to retain state for multiple requests of the same page.

The state bag is a data structure maintained by the page for retaining values between round trips to the server. By storing a value in the page's state bag, you automatically preserve it between round trips.

This section illustrates how to use page state to store the current step. Note the use of the (isPostBack!) in the Page_Load event handler in the code. This code will only execute the first time the page is loaded because each subsequent request of the page is doing a postback to the page. The panel's visible property hides and displays the two panels on the page. For more information about panels, see page 112.

WORK WITH PAGE STATE

GenericTemplate.aspx - Notepad

WORK WITH PAGE STATE

GenericTemplate.aspx - Notepad

GenericTemplate.aspx - Notepad

File Edit Format Help

<HTML> <HEAD> </HEAD> <BODY>

<FONT FACE-'Verdana">

<H3>VVelcome to mylifetimegoais.com</H3>

«FORM RUNAT="Setver"> :ASP:PANEL ID="panelStep1 " RUNAT-'SERVER''^-

You are on Step <% Re5pon5e.Write(ViewState["viewstateStep"]);%s-set your goals? Click on the Continue button to go to Step 2 P/=- _

</ASP.PANELs

<ASP:PANEL ID="panelSteb2" RUN AT-'SERVER" VISIBLE="False'

You are on Step <% Response.Write(ViewState["viewstateStep"]);S P/=-

Q Open the Template.aspx template from the directory.

Type a heading to the page.

Type a form control to the page.

_Q Type a panel to the page with an ID of panelStep1.

GenericTemplate.aspx - Notepad

File Edit Format Help

Q Open the Template.aspx template from the directory.

Type a heading to the page.

<HTML> <HEAD> </HEAD> <BODY>

<FONT FACE-'Verdana">

<H3>VVelcome to mylifetimegoais.com</H3>

«FORM RUNAT="Setver"> :ASP:PANEL ID="panelStep1 " RUNAT-'SERVER''^-

You are on Step <% Re5pon5e.Write(ViewState["viewstateStep"]);%s-set your goals? Click on the Continue button to go to Step 2 P/=- _

Are you ready to

ASP:BUTTON ID="buttonContinue" RUNAT="Server" onClick="Button_OnClick" TEXT-'Continue7>_

</ASP.PANELs

<ASP:PANEL ID="panelSteb2" RUN AT-'SERVER" VISIBLE="False'

You are on Step <% Response.Write(ViewState["viewstateStep"]);S P/=-

Type a form control to the page.

_Q Type a panel to the page with an ID of panelStep1.

Type a message about which step the user is on and output the value from the view state.

Type a button control to the page that calls the Button_OnClick when clicked.

□ Add another panel to the page with an ID of panelStep2 and set the visible property to False.

-O Add a message about which step the user is on and output the value from the view state.

ASP.NET APPLICATIONS AND STATE MANAGEMENT

Bxjra:

Page State information does not work across pages, this section features panels to display the page state information. You can test what happens when you use page state on two pages.

TYPE THIS:

<%@Page Language="C#" %> <HTML><HEAD>

<SCRIPT LANGUAGE="C#" RUNAT='Server">

void Page_Load(Object Src, EventArgs E ) {

if (!IsPostBack) ViewState["viewstateStepM] = 1;

void Button_OnClick(object Source, EventArgs e) { Response.Redirect("PageState_ai2.aspx"");

</SCRIPT></HEAD> <BODY><FONT FACE="Verdana"> <H3>Welcome to mylifetimegoals.com</H3> <FORM RUNAT="Server">

<ASP:PANEL ID="panelStep1" RUNAT="SERVER"> You are on Step <%

Response.Write(ViewState["viewstateStep"]);%>.

Are you ready to set your goals?

Click the Continue button to go to Step 2.

<ASP:BUTTON ID="buttonContinue" RUNAT="Server" onClick="Button_OnClick" TEXT="Continue"/>

RESULT:

<SCRIPT LANGUAGE="C#" RUNAT= "ServerM>

<FONT FACE='Verdana->

<H3>Welcome to mylifetimegoals.com</H3>

<FORM RUNAT='Server"> You are on Step <% Response.Write(ViewState[

GenericTemplate.aspx - Notepad

File Edit Format Help l<%@Paae Languaae="C#" %>l I

cHEADJ_

|<SCRIPT LANGUAGE-'C#" RUNAT="Server"?l_

void Page_Load(Object Src, EventArgs E ) {

if (NsPostBack) VlewState["vlewstateStep"] = 1

|void Button_OnClick(objectSource EventArgs e) { panelStepI .Visible = false; panelStep2.Visible - true,

ViewStatef'vlewstateStep"] = (lnt)VlewState["vlewstateStep"] + 1 1}_. _

<FONT FACE-'Verdana"> iH3>Welcome to myllfetimegoals.com</H3>

<FORM RUNAT="Servef>

cASP:PANEL ID="panelStep1" RUNAT="SERl/ER": You are on Step <% Response Write(ViewStato["vlewstateStep"]);0/i set your goals? Click on the Continue button to go to Step 2 iP/s

Are you ready to

_• Set the language for the page to C# using the Page directive.

_, Add the <Script> tag to set up some server-side code.

Add the Page_Load event handler and initialize the view state variable.

± Add the Button_OnClick event handler and set the first panel's visible property to false and the second panel's visible property to true. Also, increment the view state variable by 1.

Are you ready to

-E Save the file as PageState.aspx to the default Web site and request it from the Web server.

-■ The Web browser displays a message about being on step 1.

^ Click the Continue button.

-■ The Web browser displays a message about being on step 2.

The web.config file gives you a maintainable, convenient, and secure means to store configuration information. The web.config file is an XML file that stores information used to customize application level settings across your entire ASP.NET site. For example, you could store a connection string to a database.

The web.config file needs to be located in the parent directory of the ASP.NET Web site for the pages that use the application configuration information. You do not have to place the file in the root directory of your ASP.NET application. You can place a web.config file in any directory of the Web site and when a Web page accesses configuration information it will look in the parent directory. If the web.config file is not found in the parent directory, it will work its way up the directory structure until it hits the root of the Web site.

To put your own custom application settings in the web.config file you need to add the

<appSettings> tag to the root <configuration> tag. When you have done this, you can add settings by using the <add> tags and specifying the name and value of the setting (for example, <add key="pubs" value="server=(local);uid=sa;pwd=;databa se=pubs" />).

ADD APPLICATION SETTINGS

ADD APPLICATION SETTINGS

□ Open a new document in your text editor.

< Add a pair of <appSettings> tags.

_Q Add an <add> tag and set the key attribute pubs to the SQL connection string for connecting to the pubs database.

0 Save the file as web.config.

Q Open the

DatagridTemplate.aspx template from the Code Templates directory.

System.Configuration namespace.

□ Open a new document in your text editor.

Add a pair of <configuration> tags.

< Add a pair of <appSettings> tags.

_Q Add an <add> tag and set the key attribute pubs to the SQL connection string for connecting to the pubs database.

0 Save the file as web.config.

Q Open the

DatagridTemplate.aspx template from the Code Templates directory.

System.Configuration namespace.

CONFIGURE YOUR ASP.NET APPLICATIONS

Making changes to the web.config file causes it to be reloaded the next time a resource is requested. This will slow down your next request, but subsequent requests are not affected because the file is cached.

webiconfig files can contain very sensitive information that you do not want a user of your site to see. These settings could be connection strings, authorization information, MSMQ (Microsoft Messsage Queue) settings, and so on. By default, ASP.NET does not allow this file to be requested from the Web server. If users attempt to request a web.config file, they receive an HTTP access error, "This type of page is not served."

You can access configuration information by importing the System.Configuration namespace and running the following example code.

Example:

string stringPubsConnectionString = ConfigurationSettings.AppSettings["pubs"];

_0 Create a string variable and initialize the variable using the Configuration.AppSettings ["pubs"] property.

, Save the file and request it from the Web server.

■ The page appears using the SQL connection setting from the web.config file.

_0 Create a string variable and initialize the variable using the Configuration.AppSettings ["pubs"] property.

Modify the creation of the SQL Connection to use the connection string read from the configuration file.

, Save the file and request it from the Web server.

■ The page appears using the SQL connection setting from the web.config file.

SET STANDARD CONFIGURATION

You can use the standard configuration settings to specify how to configure your ASP.NET Web application. The web.config file is a central storage location for information that can apply to the entire site or just a section of the site. For example, you can configure how the Session state can be persisted on your Web site using the <sessionState> tag under the <system. web> section.

The mode attribute can be set to determine where a user's session is stored (mode="Inproc"). There are three modes that you can choose from: Inproc, StateServer, and SQLServer. Inprocis the fastest and least durable mode that holds Session state in the memory of the Web server process. Conversely, SQLServer is the slowest and most durable storage due to storage on a central SQL database store.

In the web.config file, you can place as many configuration settings as you desire. You are not required to stub in all the configuration settings. You are also not required to have a web.config file in your ASP.NET site. If your site accesses application settings and the web.config file does not exist, you will get a Web server error.

SET STANDARD CONFIGURATION

SET STANDARD CONFIGURATION

Q Open the

GenericTemplate.aspx template from the Code Templates directory.

< Add a message to the user.

'-Q Write the HTTP_USER_AGENT

server variable to the Web browser.

0 Save the file and request it from the Web server.

■ The error page appears because the code on the page was written in C# and C# is not set as the default language.

Q Open the

GenericTemplate.aspx template from the Code Templates directory.

Add a heading for the page.

< Add a message to the user.

'-Q Write the HTTP_USER_AGENT

server variable to the Web browser.

0 Save the file and request it from the Web server.

■ The error page appears because the code on the page was written in C# and C# is not set as the default language.

CONFIGURE YOUR ASP.NET APPLICATIONS

In the <configuration> tag, you can use debug="true" to enable ASP.NET debugging. You can specify the amount of time in seconds that ASP.NET should execute running an ASP.NET Web page before it times out and sends an error message about the timeout with the <executionTimeout> section.

Example:

<configuration>

<system.web>

<compilation defaultLanguage="C#" debug="true"/>

<httpRuntime executionTimeout="30"/>

</configuration>

_Q Create a web.config file.

, Request the file from the Web server.

■ The page appears without problems because the default language is set to C#.

_Q Create a web.config file.

Note: If you are creating a new web.config file, first add the <configuration> tags.

0 Add the

<compilation> tag and set the defaultLanguage attribute to C#.

, Request the file from the Web server.

■ The page appears without problems because the default language is set to C#.

Was this article helpful?

0 0

Post a comment