简体   繁体   English

将参数从C#传递到Jquery

[英]Pass parameters from C# into Jquery

How do you pass a parameter from C# into the value part of instead of hard coding it with different values. 如何将参数从C#传递到的部分,而不是使用不同的值对其进行硬编码。

<div class="property-container">
      <input class="progress-value" type="hidden" value="17" />           
        <div class="property-title">Test</div> 
        <div class="property-progress"></div>
    </div>    

    <div class="property-container">
        <input class="progress-value" type="hidden" value="32" />
        <div class="property-title">Test 2</div> 
        <div class="property-progress"></div>
    </div>    

    <div class="property-container">
        <input class="progress-value" type="hidden" value="24" />
        <div class="property-title">Test 3</div> 
        <div class="property-progress"></div>
    </div>    

        <script type="text/javascript" >/
            $(function () {
                // loop through each 'container'
                $(".property-container").each(function () {


                    // get the value that was rendered from the model
                    var progress = parseInt($(this).children(".progress-value").val());

                    // create the progress bar with the value
                    $(this).children(".property-progress").progressbar({ value: progress });

                });
            });

        </script>

You can use this : 您可以使用:

 $(this).children(".property-progress").progressbar({ value: '<%= MyValue() %>'});

And in the c# create a property : 并在c#创建一个property

Public string MyValue
{
  get;set;
}

Nb

If you intend to update the progressbar according to server activity - i'd suggest you to google signalR. 如果您打算根据服务器活动更新进度条-我建议您使用Google SignalR。

From your comments 根据您的评论

<input class="progress-value" type="hidden" value="17" id="Test" /> 
In c# I tried, Test.Value = "20";

You should have a runat="server" if you want to access the hidden field in your codebehind. 如果要访问后台代码中的隐藏字段,则应具有runat="server"

<input class="progress-value" runat="server" type="hidden" value="17" id="Test" /> 

 //& then in C# set it like this..

 Test.Value = "20";

In jQuery, get the value of the hidden field 在jQuery中,获取隐藏字段的值

$('.progress-value').val()

You just have to remember where the variables are set, and where the code is run. 您只需要记住变量的设置位置以及代码的运行位置。 C# runs on the server, so you can't set javascript properties directly, rather, you need to set up your HTML template so that when it is interpolated by the server the correct values are sent to the client. C#在服务器上运行,因此您不能直接设置javascript属性,而是需要设置HTML模板,以便在服务器对它进行插值时将正确的值发送到客户端。

In other words, you won't be able to set the progress bar width on the server, b/c the server will only be able to set the initial value of the progress bar. 换句话说,您将无法在服务器上设置进度条的宽度,因为服务器只能设置进度条的初始值。 It will be up to the client to increment the value. 由客户决定增加值。 Otherwise, you'd need to constantly be posting the page back to the server, which isn't what you want at all. 否则,您将需要不断将页面发布回服务器,这根本不是您想要的。

The easiest way to get the progress data from the server to the client is by setting up a web service that returns JSON, and calling it using jquery. 从服务器到客户端获取进度数据的最简单方法是设置返回JSON的Web服务,并使用jquery对其进行调用。

Here are some resources on how to do that: 以下是一些有关如何执行此操作的资源:

You should be able to decorate a method on your asp.net class with [WebMethod] to make it callable via jQuery. 您应该可以使用[WebMethod]装饰asp.net类上的方法,以使其可通过jQuery调用。

It really depends on what technology you're using to output code. 这实际上取决于您使用什么技术来输出代码。 If you're using Razor, it would be: 如果您使用的是Razor,它将是:

<input value="@ObjectName.Value">

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

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