简体   繁体   English

将 Slider 值从 jQuery 传递到代码后面

[英]Pass Slider Values from jQuery to Code Behind

I am using ASP.NET with C# as Code Behind.我使用 ASP.NET 和 C# 作为代码隐藏。 The thing is, I've put a jQuery Slider in my code but I can't get the values from the slider into C# (already looked everywhere on the web) and I need them because the Slider Values must act as filters for the project I am doing (a web where you can buy products). The thing is, I've put a jQuery Slider in my code but I can't get the values from the slider into C# (already looked everywhere on the web) and I need them because the Slider Values must act as filters for the project我正在做(web,您可以在其中购买产品)。 I know you can't get the values directly from jQuery to C#, but I've tried passing them to ASP.NET and still seems inaccessible.我知道您无法直接从 jQuery 到 C# 获取值,但我尝试将它们传递给 ASP.NET 并且似乎仍然无法访问。

  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
      $(function () {
          $("#slider-range").slider({
          range: true,
          min: 0,
          max: 3000,
          values: [ 50, 500 ],
          slide: function( event, ui ) {
              $("#amount").val(ui.values[0] + "€ - " + ui.values[1] + "€");      
      }
          });

        $( "#amount" ).val($( "#slider-range" ).slider( "values", 0 ) +
            "€ - " + $("#slider-range").slider("values", 1) + "€");
      } );
  </script>
<p>
  <asp:Label for="amount" runat="server">Precio:</asp:Label>
  <input type="text" id="amount" readonly style="border:0; color:#f6931f; font-weight:bold;">
</p>


<div id="slider-range">
</div>

Now, here it is what it looks like:现在,这就是它的样子:

图片

"amount" just showcases the values of the slider in the "Precio" Label. “金额”只是展示了“Precio”Label 中 slider 的值。 I need a variable and pass it to ASP then to C#, how can I do it?我需要一个变量并将其传递给 ASP,然后传递给 C#,我该怎么做?

So basically, when I hit the button marked in red I want to get both the minimum and maximum selected values in C#, because I have to pass them to the different layers of the project.所以基本上,当我点击标记为红色的按钮时,我想同时获得 C# 中的最小和最大选定值,因为我必须将它们传递给项目的不同层。

I have already tried using a hidden type on asp to put the jQuery values in it, but when I try to get the values from the hidden type in C# I just get nulls.我已经尝试在 asp 上使用隐藏类型将 jQuery 值放入其中,但是当我尝试从 C# 中的隐藏类型中获取值时,我只得到空值。

You need to post back to asp.net with an HTTP POST.您需要使用 HTTP POST 回帖到 asp.net。 It seem you are using Asp.Net Web Forms, so you can register to the change event of your slider.看来您正在使用 Asp.Net Web Forms,因此您可以注册到 slider 的更改事件。 Check this documentation检查此文档

You can do this with asp.net MVC:您可以使用 asp.net MVC 执行此操作:

$(".btnSend").click(function(){
  $.post("/Controller/Action", { amount: $('#amount').val() }, function(data, status){
    //Response callback
    alert("Data: " + data + "\nStatus: " + status);
  });
});

To pass data from javascript to code behind, you can put the data in an element known by the code-behind, that means the element, eg an hidden field:要将数据从 javascript 传递到代码隐藏,您可以将数据放在代码隐藏已知的元素中,这意味着元素,例如隐藏字段:

<asp:HiddenField ID="myValueField" ClientIdMode='STATIC' runat="server" />

Put the values in it like you did in your post:像您在帖子中所做的那样将值放入其中:

$("#myValueField").val(ui.values[0] + "€ - " + ui.values[1] + "€");

Make sure, the Hidden Field is inside your Page-Form-Control and your button is causing an server-side postback.确保隐藏字段位于您的 Page-Form-Control 内,并且您的按钮导致服务器端回发。

No need for any hidden field or ajax, just change your input to a textbox.不需要任何隐藏字段或 ajax,只需将您的输入更改为文本框。 Replace代替

<input type="text" id="amount" readonly style="border:0; color:#f6931f; font-weight:bold;">

with

<asp:TextBox ID="amount" ClientID="amount" ClientIDMode="static" runat="server" Style="border:0; color:#f6931f; font-weight:bold;" ReadOnly="True"/>

Then in the Code Behind, the value of the amount field will be the two values you want.然后在后面的代码中, amount字段的值将是您想要的两个值。 Parse the values out with some string manipulation, maybe:通过一些字符串操作来解析值,也许:

var amt = amount.Value; // You may need to do something more here to get the value; it's been a
                        // few years since I worked with webforms...
var split = amt.Split(" - ");
var first = split[0].Replace("€", "");
var second =split[1].Replace("€", ""); 
// Do what you need to do with first and second...

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

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