如何使用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;
}
}
这允许用户使用ONLY JavaScript切换ASP计时器控件。
我正在扩展它,但是现在,它工作得很好......如果我想要禁用计时器并且在工作时不更新更新面板,它很棒,不优雅,但它可以工作。
请注意,这适用于更新面板和名为ID = tmrFillAlerts的计时器(请参阅下面的代码)...我列出了最基本的要求...我拿出了CSS和格式,所以它会更容易遵循但是所有这些,看起来像这样......
当页面加载时,切换看起来像这样,并且在计时器运行时单击用户按钮...
然后,在单击上面的“关闭更新”后,您会看到此计时器并且计时器已停止...
您可以在下面的代码中看到上面的“关闭更新或关闭更新”ID = lblFlyOutTimerStatus。此外,上面的RED ON / OFF球低于ID = lblAlertsTotal。
当更新开启...并且您单击贝尔或信息按钮时,由于计时器而得到这样的弹出按钮,并且在我的情况下它每60秒更新一次(这显示了钟形弹出)
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 ver:4.0.30319.42000的IIS 8.5中适用于所有浏览器
这是一项正在进行的工作,很快就完成了,但发现了一些很酷的东西,我以为我会分享。希望能帮助到你!
祝好运!!
只要计时器不在母版页上,下面的工作就会起作用,因为你会收到一条错误,指出“控件集合无法修改,因为控件包含代码块(即<%...%>)。]”这个javascript在Master页面的标题中。
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了解更多详情