0

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

在第一个视图中,我有 ajax 计时器,它从 60 到 0 计数秒。时间显示在标签上,在 updatepanel 中。我会调用我的 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;
        }

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

4

1 回答 1

0

是的,我试过这个。调试后我知道,我的 c# 函数 DoSomething() 正在工作 - 但最后, View 没有改变。调用 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 事件中调用 ac# 函数

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

于 2013-03-23T10:20:41.787 回答