home *** CD-ROM | disk | FTP | other *** search
-
- <%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/util/SrcRef.aspx"%>
-
- <!-- #include virtual="/quickstart/aspplus/include/header.inc" -->
-
- <h4>Managing Application State</h4>
-
-
- <div class="indent" style="font-family:Verdana; font-size:8pt;">
- <b> <img align="middle" src="/quickstart/images/bullet.gif"> </b>
- <a class="toc2" target="content" href="#applicationstate">Using Application State</a><br>
- <b> <img align="middle" src="/quickstart/images/bullet.gif"> </b>
- <a class="toc2" target="content" href="#sessionstate">Using Session State</a><br>
- <b> <img align="middle" src="/quickstart/images/bullet.gif"> </b>
- <a class="toc2" target="content" href="#cookies">Using Client-Side Cookies</a><br>
- <b> <img align="middle" src="/quickstart/images/bullet.gif"> </b>
- <a class="toc2" target="content" href="#viewstate">Using View State</a><br>
- </div>
-
- <p>
- <hr>
-
- <!--BEGIN SECTION-->
- <a name="applicationstate">
- <span class="subhead">Using Application State</span>
- <p>
- This sample illustrates the use of Application State to read a data set in
- <b>Application_OnStart</b>.
- <p>
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/apps/application2/application2.aspx"
- ViewSource="/quickstart/aspplus/samples/apps/application2.src"
- Icon="/quickstart/aspplus/images/application2.gif"
- Caption="Application2.aspx"
- runat="server" />
- <p>
-
- As an Application and all the objects it stores should be concurrently accessed
- by different threads, it is better to store only infrequently modified data with
- application scope. Ideally an object is initalized in the Application_OnStart event
- and further on access is read-only.
- <p>
-
- In this sample a file is read in Application_OnStart (defined in the global.asax file)
- and the content is stored in a <b>DataView</b> object in the application state:
-
- <div class="code"><xmp>
- public void Application_OnStart()
- {
- DataSet ds = new DataSet();
-
- FileStream fs = new FileStream(Server.MapPath("schemadata.xml"),FileMode.Open,FileAccess.Read);
- StreamReader reader = new StreamReader(fs);
- ds.ReadXml(reader);
- fs.Close();
-
- DataView view = new DataView(ds.Tables[0]);
- Application["Source"] = view;
- }
- </xmp></div>
- <p>
-
- In the <b>Page_Load</b> event the DataView is then retrieved and used to populate a DataGrid object:
- <div class="code"><xmp>
- protected void Page_Load(Object Src, EventArgs E)
- {
- DataView Source = (DataView)(Application["Source"]);
- ...
- MyDataGrid.DataSource = Source;
- ...
- }
- </xmp></div>
- <p>
-
- The advantage of this solution is that only the first request pays the prize of retrieving
- the data. All subsequent requests will use the already existing DataView object. As the
- data is never modified after initialization, no provisions for serializing access have to
- be taken.
- <p>
-
-
-
- <!--BEGIN SECTION-->
- <a name="sessionstate">
- <span class="subhead">Using Session State</span>
- <p>
-
- This sample illustrates the use of session state to store volatile user preferences.
- <p>
-
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/apps/session1/session1.aspx"
- ViewSource="/quickstart/aspplus/samples/apps/session1.src"
- Icon="/quickstart/aspplus/images/session1.gif"
- Caption="Session1.aspx"
- runat="server" />
-
- <p>
-
- To provide individual data for a user during a session, data can be stored
- with session scope. In this sample values for user preferences are initialized
- in the <b>Session_OnStart</b> event in the global.asax file. On the
- customization page they are modified in the <b>Submit_Click</b> event handler according
- to the user input.
- <p>
-
- File global.asax:
- <div class="code"><xmp>
- public void Session_OnStart()
- {
- Session["BackColor"] = "beige";
- ...
- }
- </xmp></div>
- File customize.aspx:
- <div class="code"><xmp>
- protected void Submit_Click(Object sender, EventArgs E)
- {
- Session["BackColor"] = BackColor.Value;
- ...
-
- Response.Redirect(State["Referer"].ToString());
- }
- </xmp></div>
- <p>
-
- The individual values are retrieved via the GetStyle method:
- <div class="code"><xmp>
- protected String GetStyle(String key)
- {
- return Session[key].ToString();
- }
- </xmp></div>
-
- The GetStyle method is used to construct session specific styles:
- <div class="code"><pre>
- <style>
- body
- {
- font: <%=GetStyle("FontSize")%> <%=GetStyle("FontName")%>;
- background-color: <%=GetStyle("BackColor")%>;
- }
- a
- {
- color: <%=GetStyle("LinkColor")%>
- }
- </style>
- </pre></div>
- <p>
-
- To verify that the values are really stored with session scope,
- open the sample page twice, and change one value in the first
- browser window and refresh the second one. The second window
- picks up the changes because both browser instances share a
- common Session object.
- <p>
-
- <b>Configuring session state:</b>
- Session state features can be configured via the "<sessionstate>"
- section in a config.web file. To double the default timeout of
- 20 minutes the following can be added to the config.web of an
- application:
- <div class="code"><xmp>
- <sessionstate
- timeout="40"
- />
- </xmp></div>
-
- <p>
- By default ASP+ uses cookies to identify requests, which belong to
- one session. If cookies are not available, a session can be tracked
- by adding a session identifier to the URL. This can be enabled by:
- <div class="code"><xmp>
- <sessionstate
- cookieless="true"
- />
- </xmp></div>
-
- <p>
- By default ASP+ will store the session state in the same process,
- which processes the request, just like ASP. Additionally ASP+ can
- store session data in an external process, which can even reside
- on another machine. To enable this feature
- <ul>
- <li>start the aspstate service, either via the Services snap-in or by
- executing "net start aspstate" on the command line. The state service
- will by default listen on port 42424, to change the port modify the
- registry key for the service:
- HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\aspstate\Parameters\Port
- <li>set the "inproc" attribute of the sessionstate section to "false",
- <li>configure the "server" and "port" attributes with the values of the
- machine, on which you started aspstate.
- </ul>
-
- The following sample section assumes that the state service is running
- on the same machine as the web server ("localhost") and uses the default
- port ("42424"):
- <div class="code"><xmp>
- <sessionstate
- inproc="false"
- server="localhost"
- port="42424"
- />
- </xmp></div>
-
- Note, that, if you try the sample above with this setting, you can reset
- the web server (enter "iisreset" on the command line) and the session state
- value survives.
- <p>
-
-
-
- <!--BEGIN SECTION-->
- <a name="cookies">
- <span class="subhead">Using Client-Side Cookies</span>
- <p>
- This sample illustrates the use of client-side cookies to store volatile user preferences.
- <p>
-
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/apps/cookies1/cookies1.aspx"
- ViewSource="/quickstart/aspplus/samples/apps/cookies1.src"
- Icon="/quickstart/aspplus/images/cookies1.gif"
- Caption="Cookies1.aspx"
- runat="server" />
- <p>
-
- Storing cookies on the client is one of the methods the ASP+'s session
- state uses to associate requests with sessions. Cookies can also be used
- directly to persist data between requests, but the data is then stored
- on the client and sent to the server with every request. Browsers place
- limits on the size of a cookie, only a minimum of 4096 bytes is guaranteed
- to be acceptable.
- <p>
-
- As the data is stored on the client, the Page_Load method in the file
- cookies1.aspx checks if the client has sent a cookie. If not, a new
- cookie is created and initialized and stored on the client:
-
- <div class="code"><xmp>
- protected void Page_Load(Object sender, EventArgs E)
- {
- if (Request.Cookies["preferences"] == null)
- {
- HttpCookie cookie = new HttpCookie("preferences1");
- cookie.Values.Add("ForeColor","black");
- ...
- Response.AppendCookie(cookie);
- }
- }
- </xmp></div>
- <p>
-
- On the same page a GetStyle method is used again to provide
- the individual values stored in the cookie:
-
- <div class="code"><xmp>
- protected String GetStyle(String key)
- {
- HttpCookie cookie = Request.Cookies["preferences2"];
- if (cookie != null)
- {
- switch (key)
- {
- case "ForeColor" : return cookie.Values["ForeColor"]; break;
- ...
- }
- }
- return "";
- }
- </xmp></div>
- <p>
-
- Verify that the sample works by opening the cookies1.aspx page and modifying
- the preferences. Open the page in another window, it should pick up the new
- preferences. Close all browser windows and open the cookies1.aspx page again,
- this should delete the temporary cookie.
- The preferences have their default values again.
- <p>
-
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/apps/cookies2/cookies2.aspx"
- ViewSource="/quickstart/aspplus/samples/apps/cookies2.src"
- Icon="/quickstart/aspplus/images/cookies2.gif"
- Caption="Cookies2.aspx"
- runat="server" />
-
- <p>
-
- To make a cookie persistent between session, the <b>Expires</b> property on
- the <b>HttpCookie</b> class has to be set to a date in the future. The following
- code on the customization.aspx page is identical to the previous sample,
- with the exception of the assignment to cookie.Expires:
-
- <div class="code"><xmp>
- protected void Submit_Click(Object sender, EventArgs E)
- {
- HttpCookie cookie = new HttpCookie("preferences2");
- cookie.Values.Add("ForeColor",ForeColor.Value);
- ...
- cookie.Expires = DateTime.MaxValue; // Never Expires
-
- Response.AppendCookie(cookie);
-
- Response.Redirect(State["Referer"].ToString());
- }
- </xmp></div>
-
- Verify that the sample is working, by modifying a value, closing all browser
- windows and opening cookies2.aspx again. The window should still show the
- customized value.
- <p>
-
- <!--BEGIN SECTION-->
- <a name="viewstate">
- <span class="subhead">Using View State</span>
- <p>
- This sample illustrates the use of the view state to store request specific values.
- <p>
-
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/apps/pagestate1.aspx"
- ViewSource="/quickstart/aspplus/samples/apps/pagestate1.src"
- Icon="/quickstart/aspplus/images/pagestate1.gif"
- Caption="PageState1.aspx"
- runat="server" />
- <p>
-
- ASP+ provides the server-side notion of a view state for each control. A control can save its
- internal state between requests via the property State, an instance of the class StateBag. The
- Statebag class provides a dictionary like interface to store objects associated with a string key.
- <p>
-
- The file pagestate1.aspx displays one visible panel and stores the index of it in the view
- state of the page with the key "PanelIndex":
- <p>
- <div class="code"><xmp>
- protected void Next_Click(Object Src, EventArgs E )
- {
- String PrevPanelId = "Panel" + State["PanelIndex"].ToString();
- State["PanelIndex"] = (int)State["PanelIndex"] + 1;
- String PanelId = "Panel" + State["PanelIndex"].ToString();
- ...
- }
- </xmp></div>
-
- Note, that if you open the page several browser windows, each window initially shows the
- name panel. Also each window can independently navigate between the panels.
-
-
- <h4>Section Summary</h4>
- <ol>
- <li>Store data in application state variables, which is modified infrequently, but used often.
- <li>Store data in session state variables, which is specific to one session/user.
- The data is stored entirely on the server. Use it for short-lived, bulky and/or sensitive data.
- <li>Store small amounts of volatile data in a non-persistent cookie. The data is stored on the
- client, sent on each request to the server and expires when the client ends execution.
- <li>Store small amounts of non-volatile data in a persistent cookie. The data is stored on the
- client until it expires and sent on each request to the server.
- <li>Store small amounts of request-specific data in the view state. The data is sent from the server
- to the client and back.
- </ol>
- <p>
-
-
- <!-- #include virtual="/quickstart/aspplus/include/footer.inc" -->
-