Click here to Skip to main content
Click here to Skip to main content

Calling a C# server side method with parameters using jQuery

By , 9 Sep 2011
 

Design a Test.aspx page:

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

Design Test.aspx.cs:

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

Design the client script:

$(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)

About the Author

Phaneendra Varanasi
Software Developer TechRaq Solutions
India India
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionGood Job, I have tried your sample code, Need one minor correctionmemberketanbembi27 Mar '12 - 20:39 
GeneralRe: &lt;head runat="server"> &lt;title>Untitled Page&lt;/ti...memberkeyur soni12 Sep '11 - 23:43 
GeneralReason for my vote of 1 can not run, pls upload the full cod...memberRomanticoding9 Sep '11 - 15:25 
GeneralRe: Have u included jquery script file! If so no issue be faced....memberPhaneendra Varanasi9 Sep '11 - 17:45 
Have u included jquery script file! If so no issue be faced. Smile | :) Might u have included there should be no errors
This is the full code which i have given.

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 9 Sep 2011
Article Copyright 2011 by Phaneendra Varanasi
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid