65.9K
CodeProject is changing. Read more.
Home

Calling a C# method using jQuery on the client side

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.60/5 (5 votes)

Sep 13, 2011

CPOL
viewsIcon

17373

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. <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. 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. [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. // Remember to prefix your method with the class name of your page.
        alert(MyPage.GetServerTime().value);
    }