PageMethod.aspx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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:Yellow" id="ValueId"></span>
  62. <br />
  63. <span style="background-color:Aqua" id="ResultId"></span>
  64. </body>
  65. </html>