Click here to Skip to main content
15,860,972 members
Articles / Web Development / ASP.NET
Tip/Trick

Calling a C# method using jQuery on the client side

Rate me:
Please Sign up or sign in to vote.
4.76/5 (19 votes)
15 Sep 2011CPOL1 min read 95.2K   44   21
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().


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


C#
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
            return;
        }
        #endregion
    }
}

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


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


JavaScript
$('#adelete').click(function()
{
    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) {
        window.location.href='<%=ResolveUrl("~/Test1.aspx")%>/'+ID;
        //after success will redirect to new page
    }
}
$.ajax(options);
});

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

License

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


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

Comments and Discussions

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