简体   繁体   English

如何将JavaScript变量传递给服务器端方法

[英]How to pass a javascript variable to server side method

I am facing issue like I am unable to pass the javascript variable to server side I am aware that it is not achieveable in this scenario so I tried like setting the value to the asp hidden field using jQuery and getting the value of the label in server side but unfortunately I am getting empty value for the hidden field. 我遇到的问题是无法将javascript变量传递给服务器端,我知道在这种情况下无法实现,因此我尝试使用jQuery将值设置为asp隐藏字段并在服务器中获取标签的值方面,但不幸的是,我为隐藏字段获取了空值。 Help me on how to fix this 帮助我解决此问题

CODE

$(document).ready(function(){
  var DataID = "4325";
    testDataVal(DataID);
});

function testDataVal(DataID){

<%=RenderMethod(DataID) %>  // How to pass javascript variable to server side

}

Hidden Field Approach: 隐藏场方法:

$(document).ready(function(){
     var DataID = "4325";
    testDataVal(DataID);
});

function testDataVal(DataID){
   $("#<%=hdnDataVal.ClientID %>").val(DataID);

  alert($("#<%=hdnDataVal.ClientID %>").val(DataID));    // Here using javascript I can able to set the value and when I alert the value it is displayed

  <%=RenderMethod(hdnDataVal.Value) %>  // here the hiddenfield value is empty

}

    <asp:HiddenField runat="server" ID="hdnDataVal"  />

First of all... you should not mix server code and client code the way you're doing it. 首先...您不应该将服务器代码和客户端代码混合在一起使用。

It's a poor way to design your code. 这是设计代码的糟糕方法。 Try always to separate client and server code. 始终尝试分离客户端和服务器代码。 They execute on different moments, places and under different circumstances... having them together will eventually draw you to difficult to debug errors. 它们在不同的时刻,地点和不同的情况下执行……将它们放在一起最终会使您陷入难以调试的错误中。

I bet that the problem you're experiencing here is due to this way of coding. 我敢打赌,您在这里遇到的问题是由于这种编码方式造成的。

You say on your code snippet that 您在代码段中说

<%=RenderMethod(hdnDataVal.Value) %>  // here the hiddenfield value is empty

When your page is loading and server code is executed the code inside $(document).ready() is not fired yet, as it fires when your whole page finish loading. 当您的页面正在加载并且服务器代码被执行时,$(document).ready()内部的代码还不会被触发,因为在整个页面加载完成时都会触发。 So, your RenderMethod is firing before you put any value inside the variable. 因此, 将任何值放入变量之前 ,将触发RenderMethod。

try using $.ajax(); 尝试使用$ .ajax();

var url = 'my-url.aspx';
$.ajax({
    url: url,
    type: 'POST',
    data: {'variable_name': my_variable },
    success: function(html)
    { 
      alert('ok');
    },

});

and receiver on the server-side: 和服务器端的接收器:

string my_variable = Request.Form['variable_name'];

You can use a Page Method to make a server side call from client side. 您可以使用Page Method从客户端进行服务器端调用。 It's propably the easiest way to accomplish what you want. 这可能是完成您想要的最简单的方法。

First of all you need to include the Script Manager in your aspx page with Page Methods enabled: 首先,您需要在启用了“ Page Methods aspx页面中包括“脚本管理器”:

<asp:ScriptManager ID="scrmgr" EnablePageMethods="true" runat="server" /> 

Now you can invoke the server side method and pass it the client side data you want, with sth like this: 现在,您可以调用服务器端方法并将所需的客户端数据传递给它,如下所示:

<script type="text/javascript">
    $(document).ready(function(){
        var DataID = "4325";
        testDataVal(DataID);
    });

    function testDataVal(DataID) {
        PageMethods.RenderMethod(DataID, OnSuccess, OnError);
    }

    function OnSuccess(result) {
        alert(result);
    }

    function OnError() {
        alert('Some error has ocurred!');
    }
    </script>

OnSuccess function is invoked in case the server-side method has been succesfully called. 如果成功调用了服务器端方法,则调用OnSuccess函数。 Otherwise OnError function is invoked. 否则,将调用OnError函数。

This is how the Page Method should be declared inside the .aspx.cs file: 这是应该在.aspx.cs文件中声明Page Method

[System.Web.Services.WebMethod]
public static string RenderMethod(string dataID)
{
    return "Got here!";
}

If you place a breakpoint inside RenderMethod , then you can verify that client side data (ie value "4325") is correctly passed to it. 如果在RenderMethod内放置一个断点,则可以验证客户端数据(即值“ 4325”)是否已正确传递给它。

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

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