Click here to Skip to main content
11,500,576 members (71,884 online)
Click here to Skip to main content

Tagged as

Calling a C# method using jQuery on the client side

, 15 Sep 2011 CPOL 55.6K 43
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
            return;
        }
        #endregion
    }
}

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:

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

Share

About the Author

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

Comments and Discussions

 
GeneralMy vote of 5 Pin
Tushar_Patil4-Jul-12 20:13
memberTushar_Patil4-Jul-12 20:13 
GeneralRe: WC :) Thanks to you Brian for suggestion. Pin
Anuja Pawar Indore20-Sep-11 23:21
memberAnuja Pawar Indore20-Sep-11 23:21 
GeneralReason for my vote of 5 nice one Pin
nikhi _singh14-Feb-12 17:44
membernikhi _singh14-Feb-12 17:44 
GeneralRe: Thanks nikhi :) Pin
Anuja Pawar Indore14-Feb-12 18:46
memberAnuja Pawar Indore14-Feb-12 18:46 
Generalvery good article...Anuja mam ..... Pin
LAKHANP2226-Dec-11 3:40
groupLAKHANP2226-Dec-11 3:40 
GeneralRe: Thanks Lakhan :) Pin
Anuja Pawar Indore26-Dec-11 21:34
memberAnuja Pawar Indore26-Dec-11 21:34 
GeneralThanks Anuja for the nice post. It helped me a lot. Pin
Jadhav Ajay24-Nov-11 3:49
groupJadhav 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
groupJadhav Ajay24-Nov-11 3:47 
GeneralRe: Thanks Jadhav :) Pin
Anuja Pawar Indore24-Nov-11 18:54
memberAnuja Pawar Indore24-Nov-11 18:54 
GeneralReason for my vote of 4 nice and not lengthy Pin
Foyzul Karim4-Oct-11 18:37
memberFoyzul Karim4-Oct-11 18:37 
GeneralRe: Thanks Pin
Anuja Pawar Indore5-Oct-11 3:12
memberAnuja 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
memberDanielDyson19-Sep-11 11:59 
GeneralDavid and Brian done the changes suggested by you :) Pin
Anuja Pawar Indore14-Sep-11 1:20
memberAnuja 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
memberBrianBissell20-Sep-11 3:11 
GeneralYes will take care. Thanks for your suggestions Pin
Anuja Pawar Indore13-Sep-11 20:53
memberAnuja Pawar Indore13-Sep-11 20:53 
GeneralReason for my vote of 2 I think you're missing the point of ... Pin
Daniel15au13-Sep-11 14:56
memberDaniel15au13-Sep-11 14:56 
GeneralReason for my vote of 5 Nice ! Pin
Dilip Baboo13-Sep-11 13:13
memberDilip Baboo13-Sep-11 13:13 
GeneralReason for my vote of 4 Thanks for walking us through that, ... Pin
BrianBissell13-Sep-11 3:48
memberBrianBissell13-Sep-11 3:48 
GeneralReason for my vote of 3 A very interesting and informative a... Pin
David NMN Hill13-Sep-11 3:09
memberDavid NMN Hill13-Sep-11 3:09 

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 | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 15 Sep 2011
Article Copyright 2011 by Anuja Pawar Indore
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid