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

时间:2020-12-29 01:51:06

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. Help me on how to fix this

我面临的问题就是我无法将javascript变量传递给服务器端我知道它在这种情况下无法实现,所以我尝试使用jQuery将值设置为asp隐藏字段并获取服务器中标签的值一方但不幸的是我为隐藏的领域获得了空值。帮助我解决这个问题

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

3 个解决方案

#1


1  

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. So, your RenderMethod is firing before you put any value inside the variable.

加载页面并执行服务器代码时,$(document).ready()中的代码尚未触发,因为它会在整个页面加载完毕时触发。因此,在将任何值放入变量之前,RenderMethod会触发。

#2


1  

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'];

#3


1  

You can use a Page Method to make a server side call from client side. 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:

首先,您需要在启用了页面方法的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. Otherwise OnError function is invoked.

如果已成功调用服务器端方法,则调用OnSuccess函数。否则调用OnError函数。

This is how the Page Method should be declared inside the .aspx.cs file:

这是如何在.aspx.cs文件中声明Page方法:

[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 (i.e. value "4325") is correctly passed to it.

如果在RenderMethod中放置断点,则可以验证客户端数据(即值“4325”)是否正确传递给它。

#1


1  

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. So, your RenderMethod is firing before you put any value inside the variable.

加载页面并执行服务器代码时,$(document).ready()中的代码尚未触发,因为它会在整个页面加载完毕时触发。因此,在将任何值放入变量之前,RenderMethod会触发。

#2


1  

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'];

#3


1  

You can use a Page Method to make a server side call from client side. 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:

首先,您需要在启用了页面方法的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. Otherwise OnError function is invoked.

如果已成功调用服务器端方法,则调用OnSuccess函数。否则调用OnError函数。

This is how the Page Method should be declared inside the .aspx.cs file:

这是如何在.aspx.cs文件中声明Page方法:

[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 (i.e. value "4325") is correctly passed to it.

如果在RenderMethod中放置断点,则可以验证客户端数据(即值“4325”)是否正确传递给它。