Oct 17 2006

Ajax.NET part 3

Posted by admin under Ajax

Here we continue our Ajax.NET exploring adventures started here and in part 2.

One thing I havn't talked about yet is the code we put in Page_Load:



protected void Page_Load(object sender, EventArgs e)   
{   
	AjaxPro.Utility.RegisterTypeForAjax(typeof(_Default));   
}   

As I have said, thew cool thing about Ajax.NET is that it generate Javascriptwrappers for you to call from your own defined Javascript functions. I will describe the meaning of RegisterTypeForAjax by examplifying  a stupid design flaw in our current project we created in part 2. We have two ASPX pages, both implementing the same function:



    [AjaxPro.AjaxMethod]
    public int GetCurrentMinute()
    {
        System.Threading.Thread.Sleep(2000);
        return DateTime.Now.Minute;
    }


Now lets refactor that out to an own timeserver class:



public class TimeServer
{
    [AjaxPro.AjaxMethod]
    public int GetCurrentMinute()
    {
        System.Threading.Thread.Sleep(2000);
        return DateTime.Now.Minute;
    }

}


And here's the cool thing about Ajax.net.- we can specify that class in the RegisterTypeForAjax call - the Page_Load will look the same for default.aspx.cs and sample2.aspx.cs:



    protected void Page_Load(object sender, EventArgs e)
    {
        AjaxPro.Utility.RegisterTypeForAjax(typeof(TimeServer));
    }

Now when ASP.NET is loading default.aspx, Ajax.net will generate wrappers for all [AjaxPro.AjaxMethod] attributed functions in the TimeServer class - and the same will happen for calls to sample2.aspx.

So we just need to update our Javascript parts to call

TimeServer.GetCurrentMinute(MyUpdate_Callback)

instead of

_Default.GetCurrentMinute(MyUpdate_Callback) or

sample2.GetCurrentMinute(MyUpdate_Callback)

This feature of Ajax.net is really cool. Not having to tie callbacks to the page class but instead another class is the type of design I like!  

 

Attachments