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

时间:2014-12-06 10:35:54

标签: javascript c# asp.net

我通过点击按钮

调用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" />

相同的代码从另一个按钮点击进行调用。

&#34; myTimer&#34; 是我的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代码。

1 个答案:

答案 0 :(得分:1)

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

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

其他明智的你可以使用

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