ASP.NET Tutorial/Ajax/Refresh

Материал из .Net Framework эксперт
Версия от 14:57, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Timed refresh

   <source lang="csharp">

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="TimedRefresh" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"

"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <title>Untitled Page</title>

</head> <body>

   <form id="form1" runat="server">
       <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="True">
       </asp:ScriptManager>
       <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
           <ContentTemplate>
                   <asp:Label ID="Label1" runat="server" Font-Bold="True"></asp:Label>
                   
This time refreshes automatically every 1 second (for about 10 seconds).

<asp:Button ID="Button1" runat="server" Text="Refresh Time" />
           </ContentTemplate>
           <Triggers>
               <asp:AsyncPostBackTrigger ControlID="TimerControl1" EventName="Tick" />
           </Triggers>
           
       </asp:UpdatePanel>
       <asp:Timer ID="TimerControl1" runat="server" Interval="1000" OnTick="TimerControl1_Tick">
       </asp:Timer>
       
   </form>

</body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class TimedRefresh : System.Web.UI.Page {

   protected void Page_Load(object sender, EventArgs e)
   {
       Label1.Text = DateTime.Now.ToLongTimeString();
   }
   
   protected void TimerControl1_Tick(object sender, EventArgs e)
   {
       int tickCount = 0;
       if (ViewState["TickCount"] != null)
       {
           tickCount = (int)ViewState["TickCount"];
       }
       tickCount++;
       ViewState["TickCount"] = tickCount;
       if (tickCount > 10)
       {
           TimerControl1.Enabled = false;
       }
   }
}</source>