各位前辈好,我之前一直做C# - WinForm编程的,最近才开始接触ASP.NET,所以有些东西十分不解,希望大家能给予指导。我的Default2.aspx是这么写的:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" 
Inherits="Default2" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html  >
     <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>
               <div>
                   <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                   <ContentTemplate>
                    <asp:Label ID="Label1" runat="server" Text="This is a label!">
                    </asp:Label>
                       <asp:Timer ID="Timer1" runat="server" Interval=100 ontick="Timer1_Tick">
                       </asp:Timer>
                     </ContentTemplate>
                     </asp:UpdatePanel>
               </div>
          </form>
     </body>
</html>
然后我给Timer写了更新label1文字的代码:using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class Default2 : System.Web.UI.Page
{
    public int tTimer = 0;
    protected void Page_Load(object sender, EventArgs e)
    {    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        Label1.Text = tTimer++.ToString();
    }
}
的确是无刷新瞬间更新了,但是为什么不能第二次,第三次这样无限刷新下去?难道Timer达到指定时间后会让enable处于false的状态么?这里我十分不解,麻烦各位前辈能给说说原理和注意事项……谢谢!

解决方案 »

  1.   

    <asp:Timer放在UpdatePanel外面。如下:
    <form id="form1" runat="server">
              <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering=true >
              </asp:ScriptManager>
                   <div>
                        <asp:Timer ID="Timer1" runat="server" Interval=100 ontick="Timer1_Tick">
                        </asp:Timer>
                       <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                       <ContentTemplate>
                        <asp:Label ID="Label1" runat="server" Text="This is a label!">
                        </asp:Label>
                         </ContentTemplate>
                         </asp:UpdatePanel>
                   </div>
              </form>
      

  2.   

    应该这样:
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["tTimer"]="0";
            }
        }    protected void Timer1_Tick(object sender, EventArgs e)
        {
            ViewState["tTimer"] = (Convert.ToInt32(ViewState["tTimer"]) + 1).ToString();
            Label1.Text = ViewState["tTimer"].ToString();    }你public int tTimer = 0;这样设置,每次页面回发都清0了!!
    改成ViewState["tTimer"]即可!!