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

标签 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" />

通过另一个按钮点击调用相同的代码。

“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);

关于Javascript 函数不是从按钮单击后面的代码调用的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27330722/

相关文章:

javascript - 使用 jQuery 突出显示单词的父级?

c# - 为 jquery 文件上传编写服务器端处理程序

c# - NetworkCredential UseDefaultCredentials 不工作

Session_End 上的 asp.net session 变量

javascript - 当我省略/需要 Prop 时,为什么受歧视的工会不起作用?

javascript - 您可以将事件绑定(bind)到访问键吗?

c# - WP8 - 如何仅通过 C# 在 Windows Phone 8 中创建和打开 WebBrowser

c# - 处理大文件的 .NET Web 服务线程被中止

javascript - AngularJS forEach 和添加值

C# Selenium XPATH 基于 Span 中文本的动态按钮选择