May 26 2007

Define Custom EventArgs in AJAX 1.0 Extensions

Category: AJAX-ATLASBil@l @ 15:47

While preparing a sample application to my new article on aspalliance.com that will be published soon on Creating Client Controls in ASP.NET 2.0 AJAX 1.0 Extensions, I had to bind an event handler to a button created in the render method of the control. Usually, event handlers are called with an empty argument which is of type Sys.EventArgs. However, to fire an event handler and pass in a parameter instead of the empty EventArgs, you use something similar to:

$addHandler(btn, 'click', Function.createCallback(this._clickDelegate, "MyData")); 

In the above line of code, you use the Function.createCallback function when you want to pass in some data instead of the empty EventArgs default class. In this case, we are passing a string that has the value of "MyData".

Another better and clean approach is to define a new custom EventArgs class. This way, you will feel yourself while developing in AJAX Client Side as if you are working with ASP.NET Servrer side. The class that I will define is the ContactEventArgs class which is as follows:

Type.registerNamespace("Bhaidar.AJAXComponents");

Bhaidar.AJAXComponents.ContactEventArgs= function(data) {
 Bhaidar.AJAXComponents.ContactEventArgs.initializeBase(this);
    this._data = data;
}

Bhaidar.AJAXComponents.ContactEventArgs.prototype= {
    get_data: function() {
        return this._data;
    },
    set_data: function(value) {
        this._data = value;
    }
}

Bhaidar.AJAXComponents.ContactEventArgs.registerClass('Bhaidar.AJAXComponents.ContactEventArgs', Sys.EventArgs);
Sys.Application.notifyScriptLoaded();

The class is very simply, I simply add a property called data to hold the data that are needed to be passed. The important thing to mention in defining this class is the following two lines:

Bhaidar.AJAXComponents.ContactEventArgs.initializeBase(this);
Bhaidar.AJAXComponents.ContactEventArgs.registerClass('Bhaidar.AJAXComponents.ContactEventArgs', Sys.EventArgs);

The first calls the constructor of the base class and in the second line you specify that this class inherits from the EventArgs class.

Now attaching a handler and passing some data can be done this way:

        var contactEventArgs = new Bhaidar.AJAXComponents.ContactEventArgs();
        contactEventArgs.set_data("MyData");
       
        $addHandler(btn, 'click', Function.createCallback(this._clickDelegate, contactEventArgs));

Isn't that clean and clear?

Hope this helps,
Regards
 


 

 

Tags:

May 14 2007

AJAX Asynchronous Postback

Category: AJAX-ATLASBil@l @ 07:54

Another quick note on the event's order when for instance a button inside an UpdatePanel fires an event:

1- The Button inside the UpdatePanel is clicked, this means a new asynchronous postback request is initiated
2- PageRequestManager fires the initializeRequest event
3- PageRequestManager fires the beginRequest event
4- Request sent to the server
5- Response received from the server
5- PageRequestManager fires the pageLoading event
6- PageRequestManager fires the pageLoaded event
7- Application instance fires the Load event
8- PageRequestManager fires the endRequest event

You will notice that the Application's init event is fired only once which is the page first is requested. However, the Application Load event is fired all the time before the PageRequestManager's endRequest.

Hope this helps,
Regards

Tags:

May 14 2007

AJAX Initial Request

Category: AJAX-ATLASBil@l @ 07:47

Just a quick note to illustrate the event order of a page that has a ScriptManager control and loads for the first time:

1- Request sent to the server
2- Response received from the server
3- Application instance raises the Init event
4- Application instance raises the load event

 

Hope this helps,
Regards

Tags:

May 12 2007

ASP.NET AJAX from Scratch

Category: AJAX-ATLAS | MSDN WebCastsBil@l @ 11:20

For all new ASP.NET AJAX developers, I advise you to attend the coming web cast on Monday, May 14, 2007.

Check it more details here: Russ and Joe Developer Show: ASP.NET AJAX from Scratch

Hope this helps,
Regards

Tags: ,

May 11 2007

AJAX - Calling Webservices and Callback Functions

Category: AJAX-ATLASBil@l @ 08:11

I was playing around with creating an AJAX client class, in which I had a function to call a Webservice. I specified the callback function to run when the response is back from the Webservice.

The code is as follows:

        // Define a namespace
        Type.registerNamespace("Testing");
       
        // MaintainState constructor
        Testing.MaintainState = function(element) {
            this._element = element;
            this._text = "";
        }
       
        // Methods & Properties
        Testing.MaintainState.prototype = {
       
            get_Element:function() { return this._element; },
            set_Element:function(value) { this._element = value; },
           
            get_Text:function() { return this._text; },
            set_Text:function(value) { this._text = value; },
                       
            BeginServiceCall:function() {
                this.set_Text("Bilal");
                WebService.RunDummyMethod(this.EndServiceCall, this.OnError, this.OnTimeOut);
            },
           
            EndServiceCall:function(result) {
                var pnl=$get(this.get_Element());
                if (pnl.innerText != null) {
                    pnl.innerText = this.get_Text();
                }
                else{
                    pnl.textContent = this.get_Text();
                }
            },
                      
           OnError:function(result) {
                alert(result.get_message());
           },        
          
           OnTimeOut:function(result) {
                alert(result);
           }
        }

        Testing.MaintainState.registerClass("Testing.MaintainState");
       
        // Notify ScriptManager that this is the end of the script.
        if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

When you run the BeginServiceCall, a call to the Webservice dummy method is done and a callback method is specified which is EndServiceCall. If you notice that inside the EndServiceCall, I referenced properties on the client class "MaintainState" by using this. This would through an exception since the callback methods are not executed in the same context of the calling clas.

The problem is in here:

            EndServiceCall:function(result) {
                var pnl=$get(this.get_Element());
                if (pnl.innerText != null) {
                    pnl.innerText = this.get_Text();
                }
                else{
                    pnl.textContent = this.get_Text();
                }
            },

I am referencing the this.get_Text() property in the callback method, this in this case is null! So I will not be able to see the text that I placed in the BeginServiceCall function above!

To be able to maintain the state of the class in the callback functions, read here:

Thanks to Bipin Joshi(www.dotnetbips.com) with whom I discussed this issue (same problem happened in Bipin's lastest article) and Rob Bagby(www.robbagby.com) who gave me the right explanation why this code doesn't work as we might think it works in C# or any OO language! The solution is to use this instead:

WebService.RunDummyMethod(Function.createDelegate(this, this.EndServiceCall), Function.createDelegate(this,this.OnError), Function.createDelegate(this, this.OnTimeOut));

What happens now is that, we told the AJAX environment to run the callback method in the context of "this" class or the current class!

Now the code works fine!

Hope this helps!
Regards

Tags:

May 6 2007

Microsoft ASP.NET 2.0 Ajax - Client Library Webcasts

Category: AJAX-ATLAS | MSDN WebCastsBil@l @ 11:08

Rob Bagby, a Microsoft Developer Evangelist, is running in the coming weeks a series of Webcasts on the Client Side of ASP.NET AJAX, here is the list:

May 4 2007

MSDN WebCast : ASP.NET AJAX Client Libraries: Calling Web Services

Category: AJAX-ATLAS | MSDN WebCastsBil@l @ 16:46

I have attended the first live MSDN Webcast in my life yesterday by a great presenter Rob Bagby.

Few days ago I watched his first webcast which was ASP.NET AJAX Client Libraries: Overview, he promised to have a series of like 8+ web casts on the same topic!!

Yesterday's webcast was perfect, Rob went into so many details that are very important to know and explained in details how to call Webservices from the client side of AJAX. I advise you to check it out: ASP.NET AJAX Client Libraries: Calling Web Services

I am now attending the Best Practices for Building Next-Generation Web Applications with ASP.NET AJAX.

 

Regards

Tags: ,

May 4 2007

More on AJAX Client Side Life Cycle

Category: AJAX-ATLASBil@l @ 10:59

I must have forgot to mention there is still the Load method after PageLoaded and before EndRequest events!!

Have a graphical look at the client side page life cycle here at this nice blog post by David Barkol: ASP.NET AJAX: Client-side event viewer

 

Hope this helps,

Regards

Tags:

May 2 2007

Microsoft ASP.NET AJAX - Request Life Cycle

Category: AJAX-ATLAS | Telerik ControlsBil@l @ 21:32

I have recently started reading about ASP.NET AJAX. I have been always writing the AJAX code manually using the XMLHttpRequest object and other third-party great controls like the Telerik control!

I decided to work with the FREE Microsoft ASP.NET AJAX.

I would like to share with you a very important issue which is the request-life-cycle. The PageRequestManager that is created on the client side once partial page update is enabled, fires several events in the following order that you can attach your own custom events to:

  1. It first creates a request to the server using the WebRequest client side object
  2. InitializeRequest event fires
  3. BeginRequest event fires
  4. A callback method will be executed once the response is back from the server
  5. PageLoading event fires
  6. UpdatePanels on the page are now updated with the new content
  7. PageLoaded event fires
  8. Load
  9. EndRequest event fires

The items in bold represent the events that will fire during the life of a request from the time it gets created to the time it gets destroyed!

Hope this helps,
Regards

 

Tags: ,

Feb 4 2007

AJAX and IE 7

Category: AJAX-ATLASBil@l @ 20:36

I was all day preparing the demo code for my next article on www.aspalliance.com under the title of Introduction to JSON. I have a dropdownlist, where a user can select an item and I will show more details on the selected item. I am using AJAX and on the server I am responding by JSON-formatted text.

In IE 7, only first time I select an item works fine, I do an AJAX request and the details are shown! The second time it fails as if the OnReadyStateChange is not being called. It works fine for FireFox!

I contacted some help mainly Christian Wenz, the author of Programming ATLAS and the expert in ATLAS, he informed me that IE has a problem in recycling the existing XMLHttpRequest object.

That is why, whenever you are using XMLHttpRequest object, make sure you initialize it inside method that is doing the request and not outside the method, I mean in a global fashion. This way you make sure it runs on IE, FF, and IE7.

Example:

var postRequest;
function GetStudents() {
       postRequest = CreateXMLHttpRequest();
}
 

CreateXMLHttpRequest would be a helper function that creates a new XMLHttpRequest if usinf FF and IE7 and uses one of the ActiveXObjects in the older vresions of IE.

 

Hope this helps,

Regards 

 

Tags: