如何通过一个javascript变量服务器端方法 [英] How to pass a javascript variable to server side method

查看:159
本文介绍了如何通过一个javascript变量服务器端方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在面临的问题一样,我无法通过JavaScript变量服务器端的,我知道,这是不是在这种情况下achieveable所以我试图像使用jQuery的值设置为ASP的隐藏字段和获得的价值标号在服务器端,但不幸的是,我的隐藏字段渐渐空虚值。帮助我如何解决这一问题。

code

  $(文件)。就绪(函数(){
  VAR数据ID =4325;
    testDataVal(数据ID);
});功能testDataVal(数据ID){&所述;%= RenderMethod(数据ID)%GT; //如何通过JavaScript变量到服务器端}


隐藏字段的方法:

  $(文件)。就绪(函数(){
     VAR数据ID =4325;
    testDataVal(数据ID);
});功能testDataVal(数据ID){
   $(#&下;%= hdnDataVal.ClientID%gt;中)。VAL(数据ID);  警报($(#&下;%= hdnDataVal.ClientID%gt;中。)VAL(数据ID)); //这里使用JavaScript,我可以能够设置的值,当我提醒值,则显示  &所述;%= RenderMethod(hdnDataVal.Value)%GT; //这里的hiddenfield值为空}    < ASP:HiddenField =服务器ID =hdnDataVal/>


解决方案

您可以使用页方法,使从客户端服务器端调用。这是propably来完成你想要的最简单的方法。

首先,你需要包含脚本管理与您的aspx页方法已启用:

 < ASP:的ScriptManager ID =scrmgr的EnablePageMethods =真=服务器/>

现在,你可以调用服务器端方法并传递你想要在客户端的数据,像这样某事:

 <脚本类型=文/ JavaScript的>
    $(文件)。就绪(函数(){
        VAR数据ID =4325;
        testDataVal(数据ID);
    });    功能testDataVal(数据ID){
        PageMethods.RenderMethod(数据ID,的onSuccess,的OnError);
    }    功能的onSuccess(结果){
        警报(结果);
    }    功能的OnError(){
        警报('一些错误途中发生!');
    }
    < / SCRIPT>

的onSuccess 函数的情况下调用服务器端方法已成功调用。否则的OnError 函数被调用。

这是怎么了页方法应.aspx.cs文件中声明:

  [System.Web.Services.WebMethod]
公共静态字符串RenderMethod(字符串数据ID)
{
    返回在这里得到了!
}

如果你把里面的一个断点 RenderMethod ,那么你可以验证客户端数据(即值4325)是正确地传递给它。

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

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

解决方案

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:

<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.

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

[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.

这篇关于如何通过一个javascript变量服务器端方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆