【问题标题】:Call c# function when ajax timer (countdown) shows 0当ajax计时器(倒计时)显示0时调用c#函数
【发布时间】:2013-03-22 22:30:35
【问题描述】:

我在简单的任务中遇到了大问题...在 asp.net 页面上是具有两个视图的 Multiview,

在第一个视图中,我有 ajax 计时器,它从 60 到 0 计数秒。时间显示在标签上,在更新面板中。我会调用我的 c# 函数来做一些事情并最终改变活动视图。

我该怎么做?

我尝试检查 Timer_tick 事件,如果秒 == 0,我调用函数,但它不起作用。 我也试过 Timer1.Enabled = false,但它也不起作用。

我认为,我必须使用 Javascript,但如何?我不知道在哪里以及如何...我还不知道 Javascript。

这是我的 Timer_Tick 事件(标签上的时间显示正确)

  protected void Timer1_Tick(object sender, EventArgs e)
    {
        Test t = (Test)Session["SelectedTest"];

        if (t.Remaining.Minute == 0 && t.Remaining.Second == 0)
        {
            DoSomething();
        }
        else
        {
            t.Remaining= t.Remaining.AddSeconds(-1);
            Label7.Text = t.Remaining.ToLongTimeString();
        }
    }

还有我的 DoSomething() 函数:

     public void DoSomething()
        {

// Doing a lot of things....

            MultiView1.ActiveViewIndex = 3;
        }

Function Dosomething 正常工作,我也有按钮调用此函数 - 它正在工作但如果剩余秒数 == 0,我也想调用函数。

【问题讨论】:

  • 你试过if (t.Remaining.Minute <= 0 && t.Remaining.Second <= 0) 吗?

标签: c# javascript asp.net ajax timer


【解决方案1】:

是的,我已经尝试过了。调试后我知道,我的 c# 函数 DoSomething() 正在工作 - 但最后,视图没有改变。调用 DoSomething() 后,Timer Ticking 再次...

我找到了js函数:

function stopTimer()
{
      var timer = $find("<%=Timer1.ClientID%>")
     timer._stopTimer();
}

但我不知道,我在哪里可以调用它。

编辑: 我看到,我的函数 DoSomething() 被调用了很多次。如果 Timer 显示为 0,则函数在每个滴答中调用

Edit2:菜鸟解决方案:)

  1. 添加第二个 Timer,设置 Timer2.Interval = 9999999(或其他高)
  2. 在 Timer1_Tick 事件中:

    t.Remaining = t.Remaining.AddSeconds(-1); Label7.Text = t.Remaining.ToLongTimeString();

            if (t.Remaining.Minute == 0 && t.Remaining.Second == 1) Timer2.Interval = 1000;
    
  3. 在 Timer2_Tick 中调用 c# 函数

原因:如果 Timer 是 UpdatePanel 的触发器,则不能从 Tick 事件中调用 c# 函数

**我知道,这是更好的解决方案(可能是 JavaScript),但我的截止日期很快,所以我必须使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多