UpdatePanelUpdateModeCS.aspx 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <%@ Page Language="C#" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <script runat="server">
  4. protected DateTime LastUpdate
  5. {
  6. get
  7. {
  8. return (DateTime)(ViewState["LastUpdate"] ?? DateTime.Now);
  9. }
  10. set
  11. {
  12. ViewState["LastUpdate"] = value;
  13. }
  14. }
  15. protected void Button1_Click(object sender, EventArgs e)
  16. {
  17. if (LastUpdate.AddSeconds(5.0) < DateTime.Now)
  18. {
  19. UpdatePanel1.Update();
  20. LastUpdate = DateTime.Now;
  21. }
  22. }
  23. protected void Page_Load(object sender, EventArgs e)
  24. {
  25. ScriptManager1.RegisterAsyncPostBackControl(Button1);
  26. if (!IsPostBack)
  27. {
  28. LastUpdate = DateTime.Now;
  29. }
  30. }
  31. </script>
  32. <html xmlns="http://www.w3.org/1999/xhtml">
  33. <head id="Head1" runat="server">
  34. <title>UpdatePanelUpdateMode Example</title>
  35. </head>
  36. <body>
  37. <form id="form1" runat="server">
  38. <div>
  39. <asp:ScriptManager ID="ScriptManager1"
  40. runat="server" />
  41. <asp:Panel ID="Panel1"
  42. GroupingText="UpdatePanel1"
  43. runat="server">
  44. <asp:UpdatePanel ID="UpdatePanel1"
  45. UpdateMode="Conditional"
  46. runat="server">
  47. <ContentTemplate>
  48. <p>
  49. The content in this UpdatePanel only refreshes if five or more
  50. seconds have passed since the last refresh and the button in
  51. UpdatePanel2 was clicked. The time is checked
  52. server-side and the UpdatePanel.Update() method is called. Last
  53. updated: <strong>
  54. <%= LastUpdate.ToString() %>
  55. </strong>
  56. </p>
  57. </ContentTemplate>
  58. </asp:UpdatePanel>
  59. </asp:Panel>
  60. <asp:Panel ID="Panel2"
  61. GroupingText="UpdatePanel2"
  62. runat="server">
  63. <asp:UpdatePanel ID="UpdatePanel2"
  64. runat="server">
  65. <ContentTemplate>
  66. <p>
  67. This UpdatePanel always refreshes if the button is clicked.
  68. Last updated: <strong>
  69. <%= DateTime.Now.ToString() %>
  70. </strong>
  71. </p>
  72. </ContentTemplate>
  73. </asp:UpdatePanel>
  74. </asp:Panel>
  75. <asp:Button ID="Button1" Text="Button1" runat="server" OnClick="Button1_Click" />
  76. </div>
  77. </form>
  78. </body>
  79. </html>