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

Tagged as

Calling a C# method using jQuery on the client side

, 15 Sep 2011 CPOL
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
Technical Lead 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 PinmemberTushar_Patil4-Jul-12 20:13 
GeneralRe: WC :) Thanks to you Brian for suggestion. PinmemberAnuja Pawar Indore20-Sep-11 23:21 
GeneralReason for my vote of 5 nice one Pinmembernikhi _singh14-Feb-12 17:44 
GeneralRe: Thanks nikhi :) PinmemberAnuja Pawar Indore14-Feb-12 18:46 
Generalvery good article...Anuja mam ..... PingroupLAKHANP2226-Dec-11 3:40 
GeneralRe: Thanks Lakhan :) PinmemberAnuja Pawar Indore26-Dec-11 21:34 
GeneralThanks Anuja for the nice post. It helped me a lot. PingroupJadhav Ajay24-Nov-11 3:49 
GeneralReason for my vote of 5 Thanks for the nice post Anuja. I ju... PingroupJadhav Ajay24-Nov-11 3:47 
GeneralRe: Thanks Jadhav :) PinmemberAnuja Pawar Indore24-Nov-11 18:54 
GeneralReason for my vote of 4 nice and not lengthy PinmemberFoyzul Karim4-Oct-11 18:37 
GeneralRe: Thanks PinmemberAnuja Pawar Indore5-Oct-11 3:12 
GeneralReason for my vote of 1 Using an aspx page with the whole pa... PinmemberDanielDyson19-Sep-11 11:59 
GeneralDavid and Brian done the changes suggested by you :) PinmemberAnuja Pawar Indore14-Sep-11 1:20 
GeneralRe: Hey just reread the article and looks a lot cleaner and easi... PinmemberBrianBissell20-Sep-11 3:11 
GeneralYes will take care. Thanks for your suggestions PinmemberAnuja Pawar Indore13-Sep-11 20:53 
GeneralReason for my vote of 2 I think you're missing the point of ... PinmemberDaniel15au13-Sep-11 14:56 
GeneralReason for my vote of 5 Nice ! PinmemberDilip Baboo13-Sep-11 13:13 
GeneralReason for my vote of 4 Thanks for walking us through that, ... PinmemberBrianBissell13-Sep-11 3:48 
GeneralReason for my vote of 3 A very interesting and informative a... PinmemberDavid 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 | Mobile
Web01 | 2.8.141015.1 | Last Updated 15 Sep 2011
Article Copyright 2011 by Anuja Pawar Indore
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid