home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / aspplus / doc / stateoverview.aspx < prev    next >
Encoding:
Text File  |  2000-06-08  |  12.1 KB  |  367 lines

  1.  
  2. <%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/util/SrcRef.aspx"%>
  3.  
  4. <!-- #include virtual="/quickstart/aspplus/include/header.inc" -->
  5.  
  6. <h4>Managing Application State</h4>
  7.  
  8.  
  9. <div class="indent" style="font-family:Verdana; font-size:8pt;">
  10.     <b> <img align="middle" src="/quickstart/images/bullet.gif">  </b>
  11.     <a class="toc2" target="content" href="#applicationstate">Using Application State</a><br>
  12.     <b> <img align="middle" src="/quickstart/images/bullet.gif">  </b>
  13.     <a class="toc2" target="content" href="#sessionstate">Using Session State</a><br>
  14.     <b> <img align="middle" src="/quickstart/images/bullet.gif">  </b>
  15.     <a class="toc2" target="content" href="#cookies">Using Client-Side Cookies</a><br>
  16.     <b> <img align="middle" src="/quickstart/images/bullet.gif">  </b>
  17.     <a class="toc2" target="content" href="#viewstate">Using View State</a><br>
  18. </div>
  19.  
  20. <p>
  21. <hr>
  22.  
  23. <!--BEGIN SECTION-->
  24. <a name="applicationstate">
  25. <span class="subhead">Using Application State</span>
  26. <p>
  27. This sample illustrates the use of Application State to read a data set in 
  28. <b>Application_OnStart</b>.
  29. <p>
  30. <Acme:SourceRef 
  31.   RunSample="/quickstart/aspplus/samples/apps/application2/application2.aspx" 
  32.   ViewSource="/quickstart/aspplus/samples/apps/application2.src"
  33.   Icon="/quickstart/aspplus/images/application2.gif"
  34.   Caption="Application2.aspx"
  35.   runat="server" />
  36. <p>
  37.  
  38. As an Application and all the objects it stores should be concurrently accessed
  39. by different threads, it is better to store only infrequently modified data with
  40. application scope. Ideally an object is initalized in the Application_OnStart event
  41. and further on access is read-only.
  42. <p>
  43.  
  44. In this sample a file is read in Application_OnStart (defined in the global.asax file)
  45. and the content is stored in a <b>DataView</b> object in the application state:
  46.  
  47. <div class="code"><xmp>
  48. public void Application_OnStart() 
  49. {
  50.     DataSet ds = new DataSet();
  51.  
  52.     FileStream fs = new FileStream(Server.MapPath("schemadata.xml"),FileMode.Open,FileAccess.Read);
  53.     StreamReader reader = new StreamReader(fs);
  54.     ds.ReadXml(reader);
  55.     fs.Close();
  56.  
  57.     DataView view = new DataView(ds.Tables[0]);
  58.     Application["Source"] = view;
  59. }
  60. </xmp></div>
  61. <p>
  62.  
  63. In the <b>Page_Load</b> event the DataView is then retrieved and used to populate a DataGrid object:
  64. <div class="code"><xmp>
  65. protected void Page_Load(Object Src, EventArgs E) 
  66. {
  67.     DataView Source = (DataView)(Application["Source"]);
  68.     ...
  69.     MyDataGrid.DataSource = Source;
  70.     ...
  71. }
  72. </xmp></div>
  73. <p>
  74.  
  75. The advantage of this solution is that only the first request pays the prize of retrieving
  76. the data. All subsequent requests will use the already existing DataView object. As the
  77. data is never modified after initialization, no provisions for serializing access have to
  78. be taken.
  79. <p>
  80.  
  81.  
  82.  
  83. <!--BEGIN SECTION-->
  84. <a name="sessionstate">
  85. <span class="subhead">Using Session State</span>
  86. <p>
  87.  
  88. This sample illustrates the use of session state to store volatile user preferences.
  89. <p>
  90.  
  91. <Acme:SourceRef 
  92.   RunSample="/quickstart/aspplus/samples/apps/session1/session1.aspx" 
  93.   ViewSource="/quickstart/aspplus/samples/apps/session1.src"
  94.   Icon="/quickstart/aspplus/images/session1.gif"
  95.   Caption="Session1.aspx"
  96.   runat="server" />
  97.  
  98. <p>
  99.  
  100. To provide individual data for a user during a session, data can be stored
  101. with session scope. In this sample values for user preferences are initialized
  102. in the <b>Session_OnStart</b> event in the global.asax file. On the 
  103. customization page they are modified in the <b>Submit_Click</b> event handler according 
  104. to the user input.
  105. <p>
  106.  
  107. File global.asax:
  108. <div class="code"><xmp>
  109. public void Session_OnStart() 
  110. {
  111.     Session["BackColor"] = "beige";
  112.     ...  
  113. }
  114. </xmp></div>
  115. File customize.aspx:
  116. <div class="code"><xmp>
  117. protected void Submit_Click(Object sender, EventArgs E)
  118. {
  119.     Session["BackColor"] = BackColor.Value;
  120.     ...
  121.   
  122.     Response.Redirect(State["Referer"].ToString());
  123. }
  124. </xmp></div>
  125. <p>
  126.  
  127. The individual values are retrieved via the GetStyle method:
  128. <div class="code"><xmp>
  129. protected String GetStyle(String key)
  130. {
  131.     return Session[key].ToString();       
  132. }
  133. </xmp></div>
  134.  
  135. The GetStyle method is used to construct session specific styles:
  136. <div class="code"><pre>
  137. <style>
  138.     body 
  139.     {
  140.       font: <%=GetStyle("FontSize")%> <%=GetStyle("FontName")%>;
  141.       background-color: <%=GetStyle("BackColor")%>;
  142.     }
  143.     a 
  144.     { 
  145.         color: <%=GetStyle("LinkColor")%> 
  146.     }
  147. </style>
  148. </pre></div>
  149. <p>
  150.  
  151. To verify that the values are really stored with session scope, 
  152. open the sample page twice, and change one value in the first
  153. browser window and refresh the second one. The second window
  154. picks up the changes because both browser instances share a 
  155. common Session object.
  156. <p>
  157.  
  158. <b>Configuring session state:</b> 
  159. Session state features can be configured via the "<sessionstate>" 
  160. section in a config.web file. To double the default timeout of 
  161. 20 minutes the following can be added to the config.web of an
  162. application:
  163. <div class="code"><xmp>
  164. <sessionstate 
  165.   timeout="40" 
  166. />
  167. </xmp></div>
  168.  
  169. <p>
  170. By default ASP+ uses cookies to identify requests, which belong to
  171. one session. If cookies are not available, a session can be tracked
  172. by adding a session identifier to the URL. This can be enabled by:
  173. <div class="code"><xmp>
  174. <sessionstate 
  175.   cookieless="true" 
  176. />
  177. </xmp></div>
  178.  
  179. <p>
  180. By default ASP+ will store the session state in the same process, 
  181. which processes the request, just like ASP. Additionally ASP+ can
  182. store session data in an external process, which can even reside
  183. on another machine. To enable this feature
  184. <ul>
  185. <li>start the aspstate service, either via the Services snap-in or by
  186. executing "net start aspstate" on the command line. The state service
  187. will by default listen on port 42424, to change the port modify the
  188. registry key for the service:
  189. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\aspstate\Parameters\Port
  190. <li>set the "inproc" attribute of the sessionstate section to "false",
  191. <li>configure the "server" and "port" attributes with the values of the
  192. machine, on which you started aspstate.
  193. </ul>
  194.  
  195. The following sample section assumes that the state service is running
  196. on the same machine as the web server ("localhost") and uses the default
  197. port ("42424"):
  198. <div class="code"><xmp>
  199. <sessionstate 
  200.   inproc="false" 
  201.   server="localhost" 
  202.   port="42424" 
  203. />
  204. </xmp></div> 
  205.  
  206. Note, that, if you try the sample above with this setting, you can reset
  207. the web server (enter "iisreset" on the command line) and the session state
  208. value survives.
  209. <p>
  210.  
  211.  
  212.  
  213. <!--BEGIN SECTION-->
  214. <a name="cookies">
  215. <span class="subhead">Using Client-Side Cookies</span>
  216. <p>
  217. This sample illustrates the use of client-side cookies to store volatile user preferences.
  218. <p>
  219.  
  220. <Acme:SourceRef 
  221.   RunSample="/quickstart/aspplus/samples/apps/cookies1/cookies1.aspx" 
  222.   ViewSource="/quickstart/aspplus/samples/apps/cookies1.src"
  223.   Icon="/quickstart/aspplus/images/cookies1.gif"
  224.   Caption="Cookies1.aspx"
  225.   runat="server" />
  226. <p>
  227.  
  228. Storing cookies on the client is one of the methods the ASP+'s session 
  229. state uses to associate requests with sessions. Cookies can also be used
  230. directly to persist data between requests, but the data is then stored
  231. on the client and sent to the server with every request. Browsers place
  232. limits on the size of a cookie, only a minimum of 4096 bytes is guaranteed
  233. to be acceptable.
  234. <p>
  235.  
  236. As the data is stored on the client, the Page_Load method in the file 
  237. cookies1.aspx checks if the client has sent a cookie. If not, a new
  238. cookie is created and initialized and stored on the client:
  239.  
  240. <div class="code"><xmp>
  241. protected void Page_Load(Object sender, EventArgs E) 
  242. {
  243.     if (Request.Cookies["preferences"] == null)
  244.     {
  245.         HttpCookie cookie = new HttpCookie("preferences1");
  246.         cookie.Values.Add("ForeColor","black");
  247.         ...
  248.         Response.AppendCookie(cookie);
  249.     }
  250. }
  251. </xmp></div>
  252. <p>
  253.  
  254. On the same page a GetStyle method is used again to provide 
  255. the individual values stored in the cookie:
  256.  
  257. <div class="code"><xmp>
  258. protected String GetStyle(String key)
  259. {
  260.     HttpCookie cookie = Request.Cookies["preferences2"];
  261.     if (cookie != null)
  262.     {
  263.         switch (key)
  264.         {
  265.           case "ForeColor" : return cookie.Values["ForeColor"]; break;
  266.           ...
  267.         }
  268.     }
  269.     return "";
  270. }
  271. </xmp></div>
  272. <p>
  273.  
  274. Verify that the sample works by opening the cookies1.aspx page and modifying
  275. the preferences. Open the page in another window, it should pick up the new
  276. preferences. Close all browser windows and open the cookies1.aspx page again,
  277. this should delete the temporary cookie.
  278. The preferences have their default values again.
  279. <p>
  280.  
  281. <Acme:SourceRef 
  282.   RunSample="/quickstart/aspplus/samples/apps/cookies2/cookies2.aspx" 
  283.   ViewSource="/quickstart/aspplus/samples/apps/cookies2.src"
  284.   Icon="/quickstart/aspplus/images/cookies2.gif"
  285.   Caption="Cookies2.aspx"
  286.   runat="server" />
  287.  
  288. <p>
  289.  
  290. To make a cookie persistent between session, the <b>Expires</b> property on 
  291. the <b>HttpCookie</b> class has to be set to a date in the future. The following
  292. code on the customization.aspx page is identical to the previous sample, 
  293. with the exception of the assignment to cookie.Expires:
  294.  
  295. <div class="code"><xmp>
  296. protected void Submit_Click(Object sender, EventArgs E)
  297. {
  298.     HttpCookie cookie = new HttpCookie("preferences2");
  299.     cookie.Values.Add("ForeColor",ForeColor.Value);
  300.     ...
  301.     cookie.Expires = DateTime.MaxValue; // Never Expires
  302.  
  303.     Response.AppendCookie(cookie);
  304.  
  305.     Response.Redirect(State["Referer"].ToString());
  306. }
  307. </xmp></div>
  308.  
  309. Verify that the sample is working, by modifying a value, closing all browser
  310. windows and opening cookies2.aspx again. The window should still show the 
  311. customized value.
  312. <p>
  313.  
  314. <!--BEGIN SECTION-->
  315. <a name="viewstate">
  316. <span class="subhead">Using View State</span>
  317. <p>
  318. This sample illustrates the use of the view state to store request specific values.
  319. <p>
  320.  
  321. <Acme:SourceRef 
  322.   RunSample="/quickstart/aspplus/samples/apps/pagestate1.aspx" 
  323.   ViewSource="/quickstart/aspplus/samples/apps/pagestate1.src"
  324.   Icon="/quickstart/aspplus/images/pagestate1.gif"
  325.   Caption="PageState1.aspx"
  326.   runat="server" />
  327. <p>
  328.  
  329. ASP+ provides the server-side notion of a view state for each control. A control can save its
  330. internal state between requests via the property State, an instance of the class StateBag. The
  331. Statebag class provides a dictionary like interface to store objects associated with a string key.
  332. <p>
  333.  
  334. The file pagestate1.aspx displays one visible panel and stores the index of it in the view 
  335. state of the page with the key "PanelIndex":
  336. <p>
  337. <div class="code"><xmp>
  338. protected void Next_Click(Object Src, EventArgs E ) 
  339. {
  340.     String PrevPanelId = "Panel" + State["PanelIndex"].ToString();
  341.     State["PanelIndex"] = (int)State["PanelIndex"] + 1;
  342.     String PanelId = "Panel" + State["PanelIndex"].ToString();
  343.     ...
  344. }
  345. </xmp></div>
  346.  
  347. Note, that if you open the page several browser windows, each window initially shows the
  348. name panel. Also each window can independently navigate between the panels.
  349.  
  350.  
  351. <h4>Section Summary</h4>
  352. <ol>
  353. <li>Store data in application state variables, which is modified infrequently, but used often.
  354. <li>Store data in session state variables, which is specific to one session/user. 
  355. The data is stored entirely on the server. Use it for short-lived, bulky and/or sensitive data.
  356. <li>Store small amounts of volatile data in a non-persistent cookie. The data is stored on the 
  357. client, sent on each request to the server and expires when the client ends execution.
  358. <li>Store small amounts of non-volatile data in a persistent cookie. The data is stored on the
  359. client until it expires and sent on each request to the server.
  360. <li>Store small amounts of request-specific data in the view state. The data is sent from the server
  361. to the client and back.
  362. </ol>
  363. <p>
  364.  
  365.  
  366. <!-- #include virtual="/quickstart/aspplus/include/footer.inc" -->
  367.