为了完全清楚,我将其作为答案,但它基于 Jamieson 的答案。最终结果是添加了这个 Javascript:
<script type = "text/javascript">
/* Stop, Clear and Pause the timer displayed under the pause buttons */
var h1 = document.getElementsByTagName('h1')[0],
start = document.getElementById('start'),
stop = document.getElementById('stop'),
clear = document.getElementById('clear'),
seconds = 0, minutes = 0, hours = 0,
t;
function add() {
seconds++;
if (seconds >= 60) {
seconds = 0;
minutes++;
if (minutes >= 60) {
minutes = 0;
hours++;
}
}
document.getElementById('<%=h1.ClientID%>').innerText = (hours ? (hours > 9 ? hours : "0" + hours) : "00") + ":" + (minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00") + ":" + (seconds > 9 ? seconds : "0" + seconds);;
timer();
}
function timer() {
t = setTimeout(add, 1000);
}
function stp() {
clearTimeout(t);
}
function clr() {
document.getElementById('<%=h1.ClientID%>').innerText = "00:00:00";
seconds = 0; minutes = 0; hours = 0;
}
</script>
您需要添加的 ASP.Net 部分是:
<asp:UpdatePanel ID="UpdatePanel4" runat="server">
<ContentTemplate>
<table style="width:132px; margin-left:13px">
<tr>
<td style="text-align:center; margin-left:2px; border:double; background-color:darkcyan">
<asp:Label ID="h1" runat="server" ForeColor="White"><time>00:00:00</time></asp:Label>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
然后,我在我的 ASP 按钮中添加了这个:
onclientclick="stp();"
(如果您的按钮上已经有onclientclick,只需用分号分隔它们,您可以在onclientclick中有多个功能)
然后我需要将它添加到我的代码隐藏中的几个位置:
ScriptManager.RegisterClientScriptBlock(UpdatePanel4, this.GetType(), "script", "stp()", true);
ScriptManager.RegisterClientScriptBlock(UpdatePanel4, this.GetType(), "script", "clr()", true);
最后一块可能需要也可能不需要,这取决于你在做什么。