Blog - Microsoft .NET, ASP.NET, AJAX and more

Getting ActiveTabChanging Functionality from the AJAX TabControl

by Dave Marini 1/4/2010 8:08:00 PM

I have always considered myself to be quite client-side challenged. Having been soured to the thought of JavaScript by all the browser specific code I used to have to write years ago I swore off it and took to server-side development and never looked back. After a year’s hiatus doing WPF programming, I’ve recently found myself back in the thick of web development with new patterns and technologies at the forefront, namely the MVC framework and the ubiquity of jQuery. Before I begin this post I’d like to say that jQuery is, well, everything that is good about client-side programming in my opinion.

So recently I found the need to roll my own dirty form warning mechanism on a site I was working on. These forms contain quite a lot of fields and so there is gratuitous use of the TabControl from the Microsoft AJAX Control Toolkit. One of the requirements was to alert the user to changes made on one of the tabs and offer the option to save the changes before allowing the user to change tabs. Initially I thought that this task would be a breeze, what with the OnClientActiveTabChanged handler on the TabContainer control and all. Then the sad reality dawned on me. There is no way to cancel the changing of tabs from the client side. Like a freight train full of explosives it barrels on, destroying my workflow and with it any chance I’ll get sleep. But all is not lost, because with a few JavaScript ninja moves, some nifty jQuery (optional) and no personal life there is a clean solution to this problem, So what’s the answer? Read on for the details. More...

Shout it kick it on DotNetKicks.com Bookmark and Share

ASP.NET 4.0 AJAX – Preview 4 – JavaScript Observer Pattern

by Damien White 5/21/2009 11:54:00 AM

Examine In my last post on ASP.NET AJAX 4.0, we took a look at the new DataView ASP.NET AJAX control.  We saw that by using a DataView, we could easily bind data with JavaScript or declaratively with a few attributes.  In this post, we’ll look at another feature of the ASP.NET 4.0 AJAX Library, the Observer design pattern for plain JavaScript objects.  The pattern is implemented in the client side Sys.Observer class.  This feature is used internally within the new version of ASP.NET AJAX for live-binding and the DataView control.  

Here we will be using Preview 4 of the ASP.NET AJAX Library, which can be downloaded from CodePlex.  Remember that these components are still in "preview" mode (meaning no Microsoft support), though they are usable at your own risk.  For more information, you can check out the license on CodePlex.  I highly recommend downloading the samples available for Preview 4, which are also available at CodePlex.  The samples give you a good look at what is coming. 

In this post, we’ll take a closer look at the Sys.Observer class, witness the problems it solves, and take a look at a few examples.  More...

Shout it kick it on DotNetKicks.com Bookmark and Share

ASP.NET AJAX Control Development and the Chatty onMouseOut Event

by Damien White 3/24/2008 8:40:00 AM
Technorati Tags: ,,,

If you have been working on developing ASP.NET AJAX web controls, there is a good chance that you may have encountered some issues with the onMouseOut event.  The issue comes about when you are developing a container-type control (e.g. a Panel or DIV) and you wish to capture the mouse out event on the parent container.  A container control contains children, and what ends up happening is when you move your mouse around the control, you'll find that onMouseOut fires for the parent even though the mouse hasn't left the container.  The problem is due to event bubbling.  This probably isn't anything too new for you if you have done a lot of JavaScript and there are solutions on the Internet that discuss this issue.  However, in an ASP.NET AJAX Control, the process is similar to the straight JavaScript equivalent, but there are some changes to be aware of.

The solution involves performing a test on the element that the mouse is now on.  The element can be retrieved using the relatedTarget in FireFox or toElement in Internet Explorer.  Once we have the element, we will make sure that it isn't the element itself or a child of the element.  Remember, we only want to fire the onMouseOut event when the mouse has fully left the parent.  The setup here is that you have an ASP.NET AJAX Control that has a method named _onMouseOut where you have already created the handler and such for it.  If you aren't familiar with this, I would recommend checking out the ASP.NET AJAX Documentation under the ASP.NET AJAX Extensibility section, specifically, a good example is this one, which illustrates using the IScriptControl.

The Solution

_onMouseOut : function(e) {
    /// <summary>
    /// Handler for the Control's mouseout event
    /// </summary>
    /// <param name="e" type="Sys.UI.DomEvent">
    /// Event info
    /// </param>    
    
    // Access the raw event since we need the relatedTarget/toElement
    var ev = e.rawEvent;    
    
    // Access the DomElement; this is the parent we will be testing
    var parent = this.get_element();
    
    // Access the element the mouse is now over
    // relatedTarget = FireFox; toElement = IE
    var rel = (ev.relatedTarget) ? ev.relatedTarget : ev.toElement;
    
    // Ensure the mouse isn't still over the control
    // And make sure the mouse isn't over a child of the control
    if (parent != rel && !this._isChild(parent, rel)) {
        // TODO: Handle OnMouseOut        
    }
},
// Helper Methods 
_isChild : function(parent, child) {
    /// <summary>
    /// Helper method to determine if an element is a child of a parent DomElement
    /// </summary>
    /// <param name="parent" type="Sys.UI.DomElement">
    /// The parent element
    /// </param>          
    /// <param name="child" type="Sys.UI.DomElement">
    /// The element to check if it is a child of the parent
    /// </param>  
    
    // Make sure that the child node isn't null
    if (child != null) {
        
        // While the child still has a parent node
        while(child.parentNode) {
            // Move up the chain of parents
            child = child.parentNode;
            
            // Test if the test parent element is in the chain
            if(child == parent)
                return true;
        }
    }
    // Element isn't a child of the parent
    return false;
} 

I have added inline comments to make the code self-explanatory, I feel it is pretty straightforward to understand.  In addition to the _onMouseOut method, you can see I also added a helper method to test if an element is a child of a parent element.  You can trim these down into a single function if you'd like.  The one thing that you may find confusing is accessing the rawEvent.  Note that the event argument passed in an ASP.NET AJAX Control is of the type  Sys.UI.DomEvent, not the native DOM event, so hence the rawEvent property, which exposes this.  The DomEvent contains this property even though it isn't listed in the ASP.NET AJAX Documentation as of this writing.

I came up with this technique after doing some research on the Internet and came across this post on the CodeHead forums as well as this one on QuirksMode.org.  Using these ideas, I tweaked them to get them to work within the context of an ASP.NET AJAX Control.  

Shout it kick it on DotNetKicks.com Bookmark and Share