Click here to Skip to main content
15,903,724 members
Articles / Web Development / ASP.NET
Alternative
Tip/Trick

Calling a C# method using jQuery on the client side

Rate me:
Please Sign up or sign in to vote.
4.60/5 (5 votes)
15 Sep 2011CPOL 16.9K   5   3
Another alternative is to use AjaxPro.dll. It is a library that was developed by Michael Schwarz that you simply drop in the bin folder of your ASP.NET web applications. You can find more information or download this library at http://www.ajaxpro.info.To use it:Add an HttpHandler to your...

Another alternative is to use AjaxPro.dll. It is a library that was developed by Michael Schwarz that you simply drop in the bin folder of your ASP.NET web applications. You can find more information or download this library at http://www.ajaxpro.info.


To use it:



  1. Add an HttpHandler to your web.config. It should be the first of the HttpHandlers.
  2. XML
    <add verb="*" path="*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>

  3. Register your class for Ajax on the Page_Load() event in the .cs file.
  4. C#
    public partial class RAAdvice : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(RAAdvice));

  5. Decorate your method that you want to call from client-side JavaScript with [Ajaxpro.AjaxMethod()].
  6. C#
    [Ajaxpro.AjaxMethod()]
    public string GetServerTime()
    {
        return System.DateTime.ToString("HH:mm:ss:fff");
    }

  7. Call your method from your client-side script, i.e., in your .aspx file.
  8. C#
    // Remember to prefix your method with the class name of your page.
        alert(MyPage.GetServerTime().value);
    }

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAjaxPro is for old versions of .Net. You can just use Page M... Pin
AspDotNetDev14-Sep-11 8:32
protectorAspDotNetDev14-Sep-11 8:32 
GeneralReason for my vote of 5 Better solution than OP, wiring is m... Pin
jim lahey14-Sep-11 5:31
jim lahey14-Sep-11 5:31 
GeneralRe: Thanks for the vote. I didn't develop the ajaxpro library, b... Pin
Steven O14-Sep-11 5:54
Steven O14-Sep-11 5:54 

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.