PageMethod.aspx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <%@ Page Language="C#" %>
  2. <%@ Import Namespace="System.Web.Services" %>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <script runat="server">
  5. [WebMethod]
  6. // Get session state value.
  7. public static string GetSessionValue(string key)
  8. {
  9. return (string)HttpContext.Current.Session[key];
  10. }
  11. [WebMethod]
  12. // Set session state value.
  13. public static string SetSessionValue(string key, string value)
  14. {
  15. HttpContext.Current.Session[key] = value;
  16. return (string)HttpContext.Current.Session[key];
  17. }
  18. </script>
  19. <html xmlns="http://www.w3.org/1999/xhtml">
  20. <head id="Head1" runat="server">
  21. <title>Using Page Methods with Session State</title>
  22. <style type="text/css">
  23. body { font: 11pt Trebuchet MS;
  24. font-color: #000000;
  25. padding-top: 72px;
  26. text-align: center }
  27. .text { font: 8pt Trebuchet MS }
  28. </style>
  29. </head>
  30. <body>
  31. <h2>Using Page Methods with Session State</h2>
  32. <form id="form1" runat="server">
  33. <asp:ScriptManager ID="ScriptManager1"
  34. runat="server" EnablePageMethods="true">
  35. <Scripts>
  36. <asp:ScriptReference Path="PageMethod.js"/>
  37. </Scripts>
  38. </asp:ScriptManager>
  39. </form>
  40. <center>
  41. <table>
  42. <tr align="left">
  43. <td>Write current date and time in session state:</td>
  44. <td>
  45. <input type="Button"
  46. onclick="SetSessionValue('SessionValue', 'test value')"
  47. value="Write" />
  48. </td>
  49. </tr>
  50. <tr align="left">
  51. <td>Read current date and time from session state:</td>
  52. <td>
  53. <input type="Button"
  54. onclick="GetSessionValue('SessionValue')"
  55. value="Read" />
  56. </td>
  57. </tr>
  58. </table>
  59. </center>
  60. <hr/>
  61. <span style="background-color:Aqua" id="ResultId"></span>
  62. </body>
  63. </html>