網頁每隔10秒自動重新整理網頁,搭配UpdatePanel使用
HTML:
<body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:Timer ID="Timer1" runat="server" Interval="10000" OnTick="Timer1_Tick" oninit="Timer1_Init"> </asp:Timer> <div> <asp:Label ID="Label1" runat="server" Text="網頁開啟時間:"></asp:Label> <asp:Label ID="Label2" runat="server" Text=""></asp:Label> <p> </p> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <Triggers> <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" /> </Triggers> <ContentTemplate> <asp:Label ID="Label3" runat="server" Text="每隔10秒重新整理:"></asp:Label> <asp:Label ID="Label4" runat="server" Text=""></asp:Label> </ContentTemplate> </asp:UpdatePanel> <p> </p> </div> </form> </body>
C#:
protected void Page_Load(object sender, EventArgs e) { Label2.Text = DateTime.Now.ToString(); Label4.Text = DateTime.Now.ToString(); } protected void Timer1_Init(object sender, EventArgs e) { Timer1.Interval = 10000; //每10秒執行一次 } protected void Timer1_Tick(object sender, EventArgs e) { Label4.Text = "UpdatePanel1 refreshed at " + DateTime.Now.ToString(); }
沒有留言:
張貼留言