Tuesday, November 11, 2008

Ajax Method Using AjaxPro

Here i am explaining how we can use an AjaxMethod using AjaxPro Dll through which we can call Server sid methods from javascript.

Download AjaxPro Dll from
http://www.ajaxpro.info/

First Add reference to AjaxPro Dll,then create a Page.Code behind look like this,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxPro;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Utility.RegisterTypeForAjax(typeof(_Default));

if (!IsPostBack)
{

}
}

[AjaxMethod]
public int Sum(int a, int b)
{
return a + b;
}

}



Here in the above code Sum(int a, int b) is the AjaxMethod.Before using this we have to Register the Type for Ajax ie,Utility.RegisterTypeForAjax(typeof(_Default));

After that Create a javascript method and we can call this AjaxMethod like,

_Default.Sum(10, 5).value.

So our javascript function looks like this,

function CalculateSum()
{
alert(_Default.Sum(10, 5).value);
}

Here you will get the sum of the two values in script.
Another important thing is while creating AjaxMethod,it should be a public Method.And pass parameters from javascript as the same datatype as in the Ajax Method.By using this we can reduce unnecessary postback of the webpage.

0 comments:

Post a Comment