C# ASP.NET计时器不工作。如何每X秒调用一次函数?

C# ASP.NET计时器不工作。如何每X秒调用一次函数?,c#,asp.net,.net,web,C#,Asp.net,.net,Web,我正在ASP.NET框架中创建一个页面,需要每隔X秒更新标签中的文本。我试图创建新的线程并调用线程.Sleep(),但它不起作用,所有的东西都是在线程下面写的。Sleep不执行,我不知道为什么。所以我在网上找到了计时器。我创建了计时器,它不调用方法。如果我将其静态写入.aspx,它将工作: <asp:ScriptManager ID="ScriptManager1" runat="server" /> <asp:Timer runat="server" id="Up

我正在ASP.NET框架中创建一个页面,需要每隔X秒更新标签中的文本。我试图创建新的
线程
并调用
线程.Sleep()
,但它不起作用,所有的东西都是在线程下面写的。Sleep不执行,我不知道为什么。所以我在网上找到了计时器。我创建了计时器,它不调用方法。如果我将其静态写入.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();
    }
但是从未调用过
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);
    }
protected override void OnInit(事件参数e)
{
如果(!IsPostBack)
{
定时器timeoutTimer=新定时器();
timeoutTimer.ID=“timeoutTimer”;
timeoutTimer。间隔=10000;
timeoutTimer.Tick+=新事件处理程序(UpDartTimer\u Tick);
UpdatePanel timerUpdatePanel=新的UpdatePanel();
timerUpdatePanel.ContentTemplateContainer.Controls.Add(timeoutTimer);
ScriptManager.GetCurrent(this.Page).RegisterAsyncPostBackControl(timeoutTimer);
this.Page.Form.Controls.Add(timerUpdatePanel);
}
碱基.奥尼特(e);
}

我真的需要在我的网站上。请帮帮我。如何在ASP.NET中创建一些例程?为什么我不能使用线程。在网站上睡觉,为什么我的计时器不工作?谢谢

设置计时器属性后,您必须拨打此线路启动计时器:

timer.Start();
UPD: 我检查了你们的UI定时器,它可以工作,但每次勾号事件发生时它都会重新加载页面

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

因此,我建议使用我在另一个答案中提到的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;
        }
 }


受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!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>

$(文档).ready(函数(){
setInterval(函数(){
WebForm_GetElementById(“MainContent_Label1”).textContent=new Date().ToLocalString();
}, 1000);
});

请注意,您要查找的标签ID和元素名称是不同的!(老实说,我不知道ASP.NET为什么会更改该ID)

类似吗?您考虑过改为使用这个客户端(JS)吗?@SalahAkbari谢谢,但它不起作用,方法在启动时只调用了一次page@mjwills我不知道JSI强烈建议花点时间学习它。谢谢,我知道我必须调用Start(),但Timer不包含Start()方法。您使用哪种定时器?从哪个库?从System.Web.UI,我知道还有System.Timers.Timer,但它有不同的构造函数,并且不包含tick。我复制了您的代码,并在下面添加了注释:Label1.Text=DateTime.Now.ToLongTimeString();,但在页面加载时,TimerTick()只被调用一次,然后就没有了
       <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;
        }
 }

<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>