Click here to Skip to main content
15,884,986 members
Articles / Programming Languages / ASP
Tip/Trick

Calling a C# server side method with parameters using jQuery

Rate me:
Please Sign up or sign in to vote.
2.50/5 (2 votes)
9 Sep 2011CPOL 34.7K   5   4
How to call a C# server side method with parameters using jQuery.

Design a Test.aspx page:


ASP.NET
<asp:TextBox ID="Text1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Click" />
<br />
<div id="myDiv"></div>

Design Test.aspx.cs:


C#
[WebMethod]
public static string ServerSideMethod(string paraml)
{
    return "Message from Server" + paraml;
}

Design the client script:


JavaScript
$(document).ready(function () {
    var name = 'stupid';
    $('#<%=Button1.ClientID %>').click(function () {
        $.ajax({
            type: "POST",
            url: "Default7.aspx/ServerSideMethod",
            data: "{'paraml': " + $("#Text1").val() + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: true,
            cache: false,
            success: function (msg) {
                $('#myDiv').text(msg.d);
            }
        })
        return false;
    });
});

Now execute the script.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: &lt;head runat="server"> &lt;title>Untitled Page&lt;/ti... Pin
keyur soni12-Sep-11 23:43
keyur soni12-Sep-11 23:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.