2011-10-09 42 views

回答

1

YPU必须使用阿贾克斯技术,JQueryAsp.netYUI和API的其余部分,可以让您使用Ajax技术库。

最简单的一个在Asp.net是使用内置的Asp.net Ajax的功能加入了的ScriptManager的UpdatePanel到您的网页

8

简单的方法:

1导入的jQuery到您的网页
2-在页面这样

 [WebMethod] 
    public static string Save(string param1,string param2) 
    { 
     string result; 
     //your code must return somthing string 
     return result; 
    } 

3-在CS文件中创建乌尔功能您aspx页面创建功能

function save(){ 
var param1 = 'value 1 you need to send to the server should be string'; 
var param2 = 'value 2 you need to send to the server should be string'; 
     $.ajax({ 
        type: "POST", 
        url: "pagename.aspx/Save", 
        data: "{param1: '"+ param1 +"' , param2 :'"+ param2 +"'}", 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 
        async: false, 
        cache: false, 
        success: function(result){ 
       //Successfully gone to the server and returned with the string result of the server side function do what you want with the result 

        } 
        ,error(er) 
        { 
        //Faild to go to the server alert(er.responseText) 
        } 
      }); 
     } 

4-呼叫按钮,此功能单击

为我的代码和脚本更多的问题或描述,我在这里:)

+0

你确定数据线在这里是正确的吗?我无法理解你使用“和”符号的逻辑。它不应该是这样的:'data:'''{param1:'+ param1 +',param2:'+ param2 +'}'“'。我们只是想连接几个字符串并在最终结果周围放上“”。 – user107986

+0

以及我使用这个符号“作为stringfyed JSON对象的容器,当你把值放在你的值,如果值是一个字符串类型,你应该把它作为字符串处理,所以我们把'符号 – Marwan

+0

所以产生的数据字符串将如下所示:{param1:'blah',param2:'blahblah'}? – user107986

0

竟没有一个人回答了这个题。因为“导入jquery”,“使用ajax”等都否定了OP要求JavaScript解决方案的事实。这是javascript中的一行代码/很容易做到。

在JavaScript中,你只需要调用的方法:

PageMethods.SomeMethod('test'); 

你“的someMethod”会是这样的落后方法的代码:

[WebMethod] 
    public static string SomeMethod(string param1) 
    { 
     string result = "The test worked!"; 
     return result; 
    } 

规则: 你要落后识别代码方法与WebMethod属性。它必须是静态的。而且你必须在你的页面注册脚本经理如下:

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

因为我有一个aspx web表单页面工作做这样检索/积攒地理位置一些非常简单的JavaScript函数,我把它放在表内元素。

相关问题