簡體   English   中英

ASP.NET 計時器不起作用。 如何每X秒調用一次函數?

[英]ASP.NET Timer do not work. How to call function every X seconds?

我正在 ASP.NET Framework 中創建一個頁面,我需要每 X 秒更新一次 Label 中的文本。 我試圖創建新Thread並調用Thread.Sleep() ,但它不起作用,在 Thread.Sleep 下面寫的所有內容都不會執行,我不知道為什么。 所以我在互聯網定時器上找到了。 我創建了 Timer 並且它不調用方法。 如果我靜態地將它寫入 .aspx 它可以工作:

 <asp:ScriptManager ID="ScriptManager1" runat="server" />
     <asp:Timer runat="server" id="UpdateTimer" interval="5000" ontick="UpdateTimer_Tick" />
    <asp:UpdatePanel runat="server" id="TimedPanel" updatemode="Conditional">
        <Triggers>
            <asp:AsyncPostBackTrigger controlid="UpdateTimer" eventname="Tick" />
        </Triggers>
        <ContentTemplate>
            <asp:Label runat="server" id="DateStampLabel" />
        </ContentTemplate>
    </asp:UpdatePanel>
  protected void UpdateTimer_Tick(object sender, EventArgs e)
    {
        DateStampLabel.Text = DateTime.Now.ToString();
    }

這有效,但我想更改時間,所以我在 C# 中動態創建了它:

  if (!IsPostBack)
        {
            Timer timer = new Timer();
            timer.Tick += UpdateTimer_Tick;
            timer.Interval = 2000;
            timer.Enabled = true;
        }

但是UpdateTimer從來沒有被調用。 所以我嘗試了這個,但它仍然不起作用:

 protected override void OnInit(EventArgs e)
    {
        if (!IsPostBack)
        {
            Timer timeoutTimer = new Timer();
            timeoutTimer.ID = "timeouttimer";
            timeoutTimer.Interval = 10000;
            timeoutTimer.Tick += new EventHandler<EventArgs>(UpdarteTimer_Tick);


            UpdatePanel timerUpdatePanel = new UpdatePanel();
            timerUpdatePanel.ContentTemplateContainer.Controls.Add(timeoutTimer);


            ScriptManager.GetCurrent(this.Page).RegisterAsyncPostBackControl(timeoutTimer);


            this.Page.Form.Controls.Add(timerUpdatePanel);
        }


        base.OnInit(e);
    }

我真的需要這個在我的網站上。 請幫我。 如何在 ASP.NET 中創建一些例程? 為什么我不能在網站上使用 Thread.Sleep 以及為什么我的計時器不起作用? 謝謝

設置計時器屬性后,您必須調用此行來啟動計時器:

timer.Start();

UPD:我檢查了您的 UI 計時器,它可以工作,但是每次發生滴答事件時它都會重新加載頁面。

<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick"></asp:Timer>

並在文件背后的代碼中:

    protected void Page_Load(object sender, EventArgs e)
    {
        Timer1.Interval = 1000;
    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        Label1.Text = DateTime.Now.ToString();
    }

因此,我建議使用我在另一個答案中提到的 js 方法。

如果您只想更新頁面上計時器的間隔..您可以引用已經創建的計時器並在 Page_Init 或 Page_Load 中設置間隔,如下所示...

       <asp:ScriptManager ID="ScriptManager1" runat="server" />
     <asp:Timer runat="server" id="UpdateTimer" interval="5000" ontick="UpdateTimer_Tick"  />
    <asp:UpdatePanel runat="server" id="TimedPanel" updatemode="Conditional">
        <Triggers>
            <asp:AsyncPostBackTrigger controlid="UpdateTimer" eventname="Tick" />
        </Triggers>
        <ContentTemplate>
            <asp:Label runat="server" id="DateStampLabel" />
        </ContentTemplate>
    </asp:UpdatePanel>

 protected void Page_Load(object sender, EventArgs e)
 {
     if (!IsPostBack)
        {
            UpdateTimer.Interval = 2000;
        }
 }

如果沒問題,可以使用js代碼

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<script type="text/javascript">

    $(document).ready(function () {
        setInterval(function () {
            WebForm_GetElementById("MainContent_Label1").textContent = new Date().toLocaleString();
        }, 1000);
    });

</script>

注意您要查找的標簽 ID 和元素名稱是不同的! (老實說,我不知道為什么 ASP.NET 會更改該 ID)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM