简体   繁体   English

从ASP代码后面将参数传递给Javascript函数

[英]Pass parameter to Javascript function from ASP code behind

I have a long process and I want to show a user a progress bar where I pass a parameter (percentage) to a Javascript function in my Webform with masterpage. 我有一个漫长的过程,我想向用户显示一个进度条,我将参数(百分比)传递给我的带有母版页的Webform中的Javascript函数。

I have this: 我有这个:

<script type="text/javascript"> 
        function updateProgress(percentage) {          
            document.getElementById('ProgressBar').style.width = percentage+"%";
        }
</script>

and: 和:

<div class="progress progress-striped active progress-success" style="height: 43px">
      <div id="ProgressBar" class="progress-bar" role="progressbar" runat="server"
          aria-valuemin="0" aria-valuemax="100" style="width: 0%">
      </div>
</div>

In my code-behind, I have this to pass a parameter to the function: 在我的代码隐藏中,我有这个将参数传递给函数:

// Report progress >> ~ 18%                   
string updateProgress = "18";
ClientScript.RegisterStartupScript(this.GetType(), "updateProgress", "updateProgress('" + updateProgress + "');", true);

As I run the code, the progressbar never moves from 0%. 当我运行代码时,进度条永远不会从0%移动。 I would like to keep updating the percentage completion from code behind until it reaches 100% by calling the function again with new parameters. 我想通过使用新参数再次调用函数来继续更新代码后面的百分比完成,直到达到100%。

I have looked through forums but I cant see what I need to to to make it work. 我浏览过论坛,但我无法看到我需要做些什么才能让它发挥作用。

Any ideas? 有任何想法吗?

Try this way 试试这种方式

    string updateProgress = "18%";
    ClientScript.RegisterStartupScript(this.GetType(), "updateProgress", "updateProgress('" + updateProgress + "');", true);

Script AS 脚本AS

   <script type="text/javascript">
    function updateProgress(percentage) {
        document.getElementById("ProgressBar").style.width = percentage;
    }
  </script>

ASPX page ASPX页面

   <div class="progress progress-striped active progress-success" style="height: 43px">
        <div id="ProgressBar" runat="server" class="progress-bar" role="progressbar" aria-valuemin="0" aria-valuemax="100"
            style="width: 0px;" >
        </div>
    </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM