Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / Javascript
Tip/Trick

Server Side Parameterized Method Call Using JQuery

Rate me:
Please Sign up or sign in to vote.
4.55/5 (7 votes)
27 Mar 2014CPOL 24.7K   9   8
This demo shows how jquery calls the server side parameterized method.

Introduction

This is a C# project that demonstrates how client side scripting language (JQuery) is used to call server side parameterized method that is written in server side language (C#, VB).

Using the Code

Step 1

WebForm1.aspx page has some controls like button, textbox control. Textbox is used to get a message from client to pass it to server side method. On button click, div element shows the message received from server.

C++
<form id="form1" runat="server">
        <div>
            <asp:TextBox ID="Text1" runat="server" />
            <asp:Button ID="Button1" runat="server" Text="ClickMENow" />
            <br />
            <div id="myDiv"></div>
        </div>
 </form> 

Step 2

Use the following JQuery to make Ajax call to server side method:

C++
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
    <script>
        $(document).ready(function () {
            $('#<%=Button1.ClientID %>').click(function () {
                $.ajax({
                    type: "POST",
                    url: "WebForm1.aspx/ServerSideMethod",
                    data: JSON.stringify({ 'p': $('#Text1').val() }),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    async: true,
                    cache: false,
                    success: function (msg) {

                        $('#myDiv').text(msg.d);
                    },
                    error: function (x, e) { alert(x.responseText); }
                })
                return false;
            });
        });
    </script>

Step 3

Server side method in WebForm1.aspx.cs is followed by WebMethod attribute:

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

Points of Interest

The server side method must be static and prefixed by attribute [WebMethod].

License

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


Written By
Software Developer
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

 
QuestionMy vote 5 Pin
Chakravarthi Elchuri10-Jul-14 21:47
professionalChakravarthi Elchuri10-Jul-14 21:47 
AnswerRe: My vote 5 Pin
Manoj Kr. Verma16-Jul-14 2:28
professionalManoj Kr. Verma16-Jul-14 2:28 
QuestionWorks in IE Not in Chrome Pin
Grant Fletcher3-Apr-14 9:37
professionalGrant Fletcher3-Apr-14 9:37 
QuestionIt's not working Pin
ntuthuko-m28-Mar-14 3:05
professionalntuthuko-m28-Mar-14 3:05 
AnswerRe: It's not working Pin
Manoj Kr. Verma13-Nov-14 2:35
professionalManoj Kr. Verma13-Nov-14 2:35 
You can try on success.

use msg.d must.

JavaScript
success: function (msg) {
    $('#myDiv').text(msg.d);
}

and let me know in case of issue.

and thanks for comment.
GeneralMy vote of 1 Pin
PBGuy27-Mar-14 23:04
professionalPBGuy27-Mar-14 23:04 
GeneralToo Rudimentary Pin
MTProgrammer27-Mar-14 12:18
MTProgrammer27-Mar-14 12:18 
QuestionGreat Post Pin
KentsCode27-Mar-14 2:15
KentsCode27-Mar-14 2:15 

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.