Javascript 函数不是从按钮单击背后的代码调用的

Javascript function is not calling from code behind button click

本文关键字:背后 代码 调用 单击 按钮 函数 Javascript      更新时间:2023-09-26

我正在通过按钮单击调用javascript函数

 StringBuilder bldr = new StringBuilder();
    bldr.AppendFormat("var Timer = new myTimer({0},{1},'{2}','timerData');", this.timerStartValue, this.TimerInterval, this.txtResult.ClientID);
    bldr.Append("Timer.go()");
    ClientScript.RegisterStartupScript(this.GetType(), "TimerScript", bldr.ToString(), true);
ClientScript.RegisterHiddenField("timerData", timerStartValue.ToString());

按钮代码。

<asp:Button ID="Next" runat="server" OnClick="Button1_Click" Text="Next" 
                  Width="58px" />

相同的代码正在从另一个按钮单击调用。

"myTimer"是我的JavaScript函数...js 代码...

<script type="text/javascript">
    function myTimer(startVal, interval, outputId, dataField) {
         this.value = startVal;
         this.OutputCntrl = document.getElementById(outputId);
         this.currentTimeOut = null;
         this.interval = interval;
         this.stopped = false;
         this.data = null;
         var formEls = document.documentElement;
         if (dataField) {
             for (var i = 0; i < formEls.length - 1; i++) {
                 if (formEls[i].name == dataField) {
                     this.data = formEls[i];
                     i = formEls.length + 1;
                 }
             }
         }
         myTimer.prototype.go = function () {
             if (this.value > 0 && this.stopped == false) {
                 this.value = (this.value - this.interval);
                 if (this.data) {
                     this.data.value = this.value;
                 }
                 var current = this.value;
                 this.OutputCntrl.innerHTML = this.Hours(current) + ':' + this.Minutes(current) + ':' + this.Seconds(current);
                 this.currentTimeOut = setTimeout("Timer.go()", this.interval);
             }
             else {
                 alert('Time Out!');
                //window.location('Index.aspx');
             }

         }
         myTimer.prototype.stop = function () {
             this.stopped = true;
             if (this.currentTimeOut != null) {
                 clearTimeout(this.currentTimeout);
             }
         }
         myTimer.prototype.Hours = function (value) {
             return Math.floor(value / 3600000);
         }
         myTimer.prototype.Minutes = function (value) {
             return Math.floor((value - (this.Hours(value) * 3600000)) / 60000);
         }
         myTimer.prototype.Seconds = function (value) {
             var hoursMillSecs = (this.Hours(value) * 3600000)
             var minutesMillSecs = (this.Minutes(value) * 60000)
             var total = (hoursMillSecs + minutesMillSecs)
             var ans = Math.floor(((this.value - total) % 60000) / 1000);
             if (ans < 10)
                 return "0" + ans;
             return ans;
         }
     }  

请检查更新的代码。

我现在更新了完整的 Js 代码。

如果您使用了更新面板,则可以使用:

ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "javascriptFunction();", true);

您可以使用的其他明智

ClientScript.RegisterStartupScript
        (GetType(),Guid.NewGuid().ToString(), "javascriptFunction();",true);