Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Calling a C# method using jQuery on the client side

4.60/5 (5 votes)
15 Sep 2011CPOL 17.4K  
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)