Click here to Skip to main content
13,863,642 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


44 bookmarked
Posted 7 Sep 2011
Licenced CPOL

Calling a C# method using jQuery on the client side

, 15 Sep 2011
Rate this:
Please Sign up or sign in to vote.
Calling a C# method using jQuery on the client side.

This code will help you when you need to define a method code-behind and want to call that method from the client side. jQuery has made life simple. There is a very easy way to do this.

Earlier, we knew one way that we needed to add our method as a WebMethod if we wanted to call a method from the code-behind at the client side. Using this method, we can do it without calling our method in a WebMethod.

In this example, I am writing a method to delete a particular user when the Delete key is hit. I don't want a postback so I want the event to execute on the client side. I write a Delete method on the server side in the CS file and call it from the client side using jQuery. The name of my page is Test.aspx.

This is my method in the CS file with the name DeleteRec().

private void DeleteRec()
    int ID= Request.Form["ID"].ToString().ToInteger(); 
    //parameter send from client side
    int UserID = Request.Form["UserID "].ToString().ToInteger();
    //parameter send from client side
    UserBO lObjUserBO = new UserBO ();
    lObjUserBO .DeleteUser(ID, UserID );

Here is how we call the method on page load:

protected void Page_Load(object sender, EventArgs e)
    if (!Page.IsPostBack)
        #region Ajax methods
        if (Request.Form["MethodName"] == "DeleteR")
        // same Method Name that we are specifying on client side(DeleteR)
            DeleteRec();// Method defined on the page to delete the record

This is what we need to add at the client side (Test.aspx).

<a id="adelete" href="java<!-- no -->script:void(0);">Delete</a>

Use the following script to call the server side method when this anchor tag is clicked:

    var dataToSend={ID:ID,MethodName:'DeleteR',UserID :UserID };

    var options =
        url: '<%=ResolveUrl("~/Test.aspx") %>?x=' + new Date().getTime(),
        data: dataToSend,
        dataType: 'JSON',
        type: 'POST',
        success: function (response) {
        //after success will redirect to new page

Hope you find it useful, when trying to call a server side method client side.


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


About the Author

Anuja Pawar Indore
Program Manager Infobeans
India India
I have keen interest in learning new things, exploring more on a topic and being more versatile

You may also be interested in...


Comments and Discussions

GeneralGreat article on jQuery AJAX Pin
Yogi S.16-Jan-17 9:20
memberYogi S.16-Jan-17 9:20 
GeneralRe: Great article on jQuery AJAX Pin
Anuja Pawar Indore16-Jan-17 20:25
memberAnuja Pawar Indore16-Jan-17 20:25 
GeneralMy vote of 5 Pin
_Tushar Patil4-Jul-12 21:13
member_Tushar Patil4-Jul-12 21:13 
GeneralRe: WC :) Thanks to you Brian for suggestion. Pin
Anuja Pawar Indore21-Sep-11 0:21
memberAnuja Pawar Indore21-Sep-11 0:21 
GeneralReason for my vote of 5 nice one Pin
Nikhil_S14-Feb-12 18:44
professionalNikhil_S14-Feb-12 18:44 
GeneralRe: Thanks nikhi :) Pin
Anuja Pawar Indore14-Feb-12 19:46
memberAnuja Pawar Indore14-Feb-12 19:46 
Generalvery good article...Anuja mam ..... Pin
Lakhan Aanjana26-Dec-11 4:40
groupLakhan Aanjana26-Dec-11 4:40 
GeneralRe: Thanks Lakhan :) Pin
Anuja Pawar Indore26-Dec-11 22:34
memberAnuja Pawar Indore26-Dec-11 22:34 
GeneralThanks Anuja for the nice post. It helped me a lot. Pin
Jadhav Ajay24-Nov-11 4:49
professionalJadhav Ajay24-Nov-11 4:49 
GeneralReason for my vote of 5 Thanks for the nice post Anuja. I ju... Pin
Jadhav Ajay24-Nov-11 4:47
professionalJadhav Ajay24-Nov-11 4:47 
GeneralRe: Thanks Jadhav :) Pin
Anuja Pawar Indore24-Nov-11 19:54
memberAnuja Pawar Indore24-Nov-11 19:54 
GeneralReason for my vote of 4 nice and not lengthy Pin
Foyzul Karim4-Oct-11 19:37
professionalFoyzul Karim4-Oct-11 19:37 
GeneralRe: Thanks Pin
Anuja Pawar Indore5-Oct-11 4:12
memberAnuja Pawar Indore5-Oct-11 4:12 
GeneralReason for my vote of 1 Using an aspx page with the whole pa... Pin
DanielDyson19-Sep-11 12:59
memberDanielDyson19-Sep-11 12:59 
GeneralDavid and Brian done the changes suggested by you :) Pin
Anuja Pawar Indore14-Sep-11 2:20
memberAnuja Pawar Indore14-Sep-11 2:20 
GeneralRe: Hey just reread the article and looks a lot cleaner and easi... Pin
BrianBissell20-Sep-11 4:11
memberBrianBissell20-Sep-11 4:11 
GeneralYes will take care. Thanks for your suggestions Pin
Anuja Pawar Indore13-Sep-11 21:53
memberAnuja Pawar Indore13-Sep-11 21:53 
GeneralReason for my vote of 2 I think you're missing the point of ... Pin
Daniel Lo Nigro13-Sep-11 15:56
memberDaniel Lo Nigro13-Sep-11 15:56 
GeneralReason for my vote of 5 Nice ! Pin
Dilip Baboo13-Sep-11 14:13
memberDilip Baboo13-Sep-11 14:13 
GeneralReason for my vote of 4 Thanks for walking us through that, ... Pin
BrianBissell13-Sep-11 4:48
memberBrianBissell13-Sep-11 4:48 
GeneralReason for my vote of 3 A very interesting and informative a... Pin
David NMN Hill13-Sep-11 4:09
memberDavid NMN Hill13-Sep-11 4:09 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190214.1 | Last Updated 15 Sep 2011
Article Copyright 2011 by Anuja Pawar Indore
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid