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

Server Side Parameterized Method Call Using JQuery

, 27 Mar 2014
Rate this:
Please Sign up or sign in to vote.
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.

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

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

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

About the Author

Manoj Kr. Verma
Software Developer
India India
No Biography provided

Comments and Discussions

 
QuestionMy vote 5 PinprofessionalChakravarthi Elchuri10-Jul-14 21:47 
AnswerRe: My vote 5 PinprofessionalManoj Kr. Verma16-Jul-14 2:28 
QuestionWorks in IE Not in Chrome PinmemberGrant Fletcher3-Apr-14 9:37 
QuestionIt's not working Pinmemberntuthuko-m28-Mar-14 3:05 
GeneralMy vote of 1 PinprofessionalPBGuy27-Mar-14 23:04 
GeneralToo Rudimentary Pinmemberb-rad_mt27-Mar-14 12:18 
QuestionGreat Post PinmemberKentsCode27-Mar-14 2:15 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 27 Mar 2014
Article Copyright 2014 by Manoj Kr. Verma
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid