如何使用Javascript禁用/启用asp.net计时器控件?

时间:2011-09-26 05:02:59

标签: asp.net javascript-events

如何使用Javascript禁用/启用asp.net计时器控件 我的代码是:(但不能正常工作)

 function timeroff() 
 {
      var b = document.getElementById('Timer1');
      if (b) {
            b.disabled = true;
        }
    }
    function timeron() {
        var b = document.getElementById('Timer1');
        if (b) {
            b.disabled = false;
        }
    }

2 个答案:

答案 0 :(得分:0)

只要计时器不在母版页上,以下情况就会起作用,因为您将收到一条错误说明"控件集合无法修改,因为控件包含代码块(即<%...%> ;)]&#34。当你把这个javascript放在母版页的标题中时。

    function enableTimer() {
    var timer = Sys.Application.findComponent(‘<%= Timer1.ClientID %>’);
        timer.set_enabled(true);
    }
    function disableTimer() {
        var timer = Sys.Application.findComponent(‘<%= Timer1.ClientID %>’);
        timer.set_enabled(false);
    }

请参阅:http://weblogs.asp.net/andrewfrederick/controlling-the-asp-net-timer-control-with-javascript了解更多详情

答案 1 :(得分:0)

这允许用户仅使用JavaScript来切换ASP计时器控件。

我正在扩展它,但是现在,它的工作非常好...如果我想禁用计时器,并且在工作时不更新更新面板,那很好,还不是很优雅,但是它可以工作。

注意,这与更新面板和一个名为ID = tmrFillAlerts的计时器一起使用(请参见下面的代码)...我列出了最基本的要求...我取出了CSS并进行了格式化,因此更容易遵循,但包括所有这些,它看起来像这样……

页面加载时,切换开关如下图所示:

enter image description here

然后,在单击上方的“关闭更新”后,您会看到此消息,并且计时器已停止...

enter image description here

您可以在下面的代码中以ID = lblFlyOutTimerStatus的形式看到上面的“关闭或打开更新”。另外,上方的红色ON / OFF球也位于下方,ID = lblAlertsTotal。

当更新处于打开状态...并单击“铃声”或“信息”按钮时,由于有了计时器,您将得到类似的弹出效果,在我的情况下,每60秒更新一次(这显示了铃声弹出) enter image description here

C#背后的代码...

// Loads the DataList from the TIMER component
private void udpAlertBar(object sender, EventArgs e) {
    // Do whatever you want here on timer tick ... 

    // Write the last BELL alert time...
    lblAlertTime.Text = "Last update time:<br/>" + DateTime.Now.ToString("MM/dd/yyy hh:mm:ss tt");
    // Load the BELL and INFO icon drop down lists
    FillBellInfoDataLists();
    // Update red alert ballz .... 
    UpdateAlertBallTotals();
}

ASP页面代码...

<asp:UpdatePanel id="udpAlertItemUpdater" runat="server">
  <ContentTemplate>
     <!-- NOTE: The update panel must wrap just this area (UPDpnl kills the javascript for some reason otherwise)  -------->
     <asp:Label id="lblTimerState" runat="server" Text="on" />
        <ul>
         <li>     
           <a href="javascript:void(0)" onclick="toggleUpdateTimer()"> 
            <asp:Label ID="lblFlyOutTimerStatus" runat="server" Text="Turn updates OFF" />         
           </a>
         </li>
        </ul>
        <asp:Timer ID="tmrFillAlerts" runat="server" OnTick="udpAlertBar" Interval="60000" Enabled="true"/>
  </ContentTemplate>
</asp:UpdatePanel>

JavaScript代码...

<script type="text/javascript">
    function toggleUpdateTimer() {
        // Gets the timer control on the page named “tmrFillAlerts” 
        var timerState = $find(“tmrFillAlerts”);
        // Gets the label I use for an alert ball on an icon on the page …
        var timerStatusRedAlertBall = document.getElementById(‘lblTimerState’);
        // Gets the menu item I have on the alert icon that drops down when clicked …
        var timerStatusFlyOutLabel = document.getElementById(‘lblFlyOutTimerStatus’);
        // Toggle the timer when the menu item is clicked ….
        if (timerState.get_enabled() == true) { 
            stopUpdateTimer(); // NOTE: stop the timer, then disable …
            timerState.set_enabled(false);
            timerStatusRedAlertBall.innerHTML = “OFF”;
            timerStatusFlyOutLabel.innerHTML = “Turn Updates ON”;}
        else { 
            timerState.set_enabled(true); // NOTE: Enable the timer, then start ….
            startUpdateTimer();
            timerStatusRedAlertBall.innerHTML = “on”;
            timerStatusFlyOutLabel.innerHTML = “Turn Updates OFF”;}
    }

    function stopUpdateTimer() {
            var timer = $find(“tmrFillAlerts”);
            timer._stopTimer();
    }

    function startUpdateTimer() {
            var timer = $find(“tmrFillAlerts”);
            timer._startTimer();
    }
</script>

我删除了除相关项目以外的所有内容,以使其正常工作……否则,这将长达十页!!

当前从所有运行NET版本:4.0.30319.42000的IIS 8.5的浏览器中为我工作

这是一个正在进行的工作,很快就完成了,但是发现了一些很酷的东西,我想我愿意分享。希望能帮助到你!

祝你好运!