Book Image

Microsoft AJAX Library Essentials: Client-side ASP.NET AJAX 1.0 Explained

Book Image

Microsoft AJAX Library Essentials: Client-side ASP.NET AJAX 1.0 Explained

Overview of this book

Microsoft AJAX Library Essentials is a practical reference for the client-side library of the ASP.NET AJAX Framework 1.0, and a tutorial for the underlying technologies and techniques required to use the library at its full potential. The main goal of this book is to get you comfortable with the Microsoft AJAX Library, a huge set of functions that can be used for developing powerful client-side functionality.Beginning with a hands-on tour of the basic technologies associated with AJAX, JavaScript, XMLHttpRequest, JSON, and the DOM, you'll move on to a crash course in the Microsoft AJAX tools. You will learn, through numerous step-by-step exercises, how to create basic AJAX applications, how the object-based programming model of JavaScript works, and how Microsoft AJAX Library extends this model. You'll understand the architecture of the Microsoft AJAX components, how they all fit together, and exactly what they can do for you. Then you will learn how to use the Microsoft AJAX Library in your web projects, and a detailed case study will walk you through creating your own customized client components. At every stage of your journey, you'll be able to try out examples to illuminate the theory, and consolidate your understanding. In addition to learning about the client and server controls, you'll also see how to handle errors and debug your AJAX applications.To complement your new found skills, the book ends with a visual reference of the Microsoft AJAX Library namespaces and classes, including diagrams and quick explanations for all the classes mentioned in the book, providing an invaluable reference you will turn to again and again.
Table of Contents (14 chapters)
Copyright
Credits
About the Authors
About the Reviewers
Preface

Sys.UI Namespace


We cover the following members of Sys.UI:

  • Sys.UI.Behavior

  • Sys.UI.Bounds

  • Sys.UI.DomElement

  • Sys.UI.DomEvent

  • Sys.UI.Key

  • Sys.UI.MouseButton

  • Sys.UI.Point

  • Sys.UI.VisibilityMode

Sys.UI.Behavior Class

Sys.UI.Behavior class (Figure A-34) extends Sys.Component and represents the base class for all behaviors.

Figure A-34 Sys.UI.Behavior

Sys.UI.Bounds Class

Sys.UI.Bounds (Figure A-35) contains information about a point’s position, a width, and a height.

Figure A-35 Sys.UI.Bounds

Sys.UI.Control Class

Sys.UI.Bounds class (Figure A-36) extends Sys .Component and represents the base class for all controls.

Figure A-36 Sys.UI.Bounds

Sys.UI.DomElement Class

Sys.UI.Bounds class (Figure A-37) a set of methods for operating on a DOM element.

Figure A-37 Sys.UI.Bounds

getElementById() Method ($get)

Static method that searches a DOM element by its ID.

Parameters

id – the ID of the DOM element to be searched for.

element – optional DOM element that specifies parent element to search in.

Remarks

If the DOM element to search in is not specified, the document element is presumed by default.

Example
//Get a DOM element
var elm=$get('log');

addCssClass() Method

Static method that add a CSS class to a DOM element.

Parameters

element – the DOM element.

className – the name of the CSS class.

Example
//Add a CSS class
Sys.UI.DomElement.addCssClass($get('log'),'newClass');

containsCssClass() Method

Static method that checks to see if a DOM element has a specified CSS class.

Parameters

element – the DOM element.

className – the name of the CSS class.

Returns

The method returns true if the DOM element has the specified CSS class or false otherwise.

Example
//Check to see if a DOM element has a CSS class
Sys.Debug.trace(Sys.UI.DomElement.containsCssClass($get('log'),
'class'));

removeCssClass() Method

Static method that removes a CSS class from a DOM element.

Parameters

element – the DOM element.

className – the name of the CSS class.

Example
//Remove a CSS class from a DOM element
Sys.UI.DomElement.removeCssClass($get('log'),'class');

toggleCssClass() Method

Static method that toggles a CSS class for a DOM element.

Parameters

element – the DOM element.

className – the name of the CSS class.

Example
//Toggle a CSS class
Sys.UI.DomElement.toggleCssClass($get('log'),'class');

getLocation() Method

Static method that gets the position of the upper left corner for a DOM element.

Parameters

element – the DOM element.

Returns

The method returns a Sys.UI.Point object containing the upper left corner’s position.

getBounds() Method

Static method that gets the bounds for a DOM element.

Parameters

element – The DOM element.

Returns

The method returns a Sys.UI.Bounds object containing the position and dimensions of the DOM element.

Remarks

The method internally uses Sys.UI.DomElement.getLocation() for the x and y fields of the Sys.UI.Bounds object and the offsetWidth and offsetHeight attributes of the DOM element for the width and height fields of the same Sys.UI.Bounds object.

For more information about the boxing model please check:

http://www.w3.org/TR/CSS21/box.html
Example
//Toggle a CSS class
Sys.UI.DomElement.toggleCssClass($get('log'),'class');

setLocation() Method

Static method that sets the absolute position for a DOM element.

Parameters

element – the DOM element.

x – number of horizontal pixels from the top left corner.

y – number of vertical pixels from the top left upper corner.

Remarks

The method internally sets the position CSS attribute to absolute and the left and top attributes to x and y.

Example
//Set the location for a DOM element
Sys.UI.DomElement.setLocation($get('log'),10,20);

Sys.UI.DomEvent Class

Sys.UI.DomEvent (Figure A-38) stores all the information that is passed to a handler registered to a DOM event and also provides methods for registering to a DOM event.

Figure A-38 Sys.UI.DomEvent

Sys.UI.Key Class

This enumeration contains the key codes.

Figure A-39 Sys.UI.DomEvent

addHandler() Method ($addHandler)

Static method that adds a handler to an event of a DOM element.

Parameters

element – the DOM element.

eventName – the name of the event the handler is being attached to.

handler – the handler to be attached to the DOM element’s event.

Remarks

In the event handler, the this keyword refers to the DOM element. Use Function. createDelegate() to create handler delegates inside objects so that this refers the object and not the DOM element.

Example
// Add a handler for the keypress event
// Version 1
    this._onKeyPressHandler = Function.createDelegate(this, this._
onKeyPress);
    $addHandler(this.get_element(),'keypress',this._
onKeyPressHandler);
// Same as Sys.DomEvent.addHandler
...
_onKeyPress: function(e){..}


// Version 2
$addHandler(element, 'keypress', handler);
function handler(e){..}

addHandlers() Method ($addHandlers)

Static method that adds a series of handlers to different events of a DOM element.

Parameters

element – the DOM element.

events – a JSON object containing pairs of event names and their handlers.

handlerOwner – if specified, this will point to it inside event handlers.

Remarks

There is no need to create delegates for the event handlers as with the addHandler(). This method internally uses Function.createDelegate() if handlerOwner is specified.

This method could be used inside the initialize() method of a component when a series of event handlers need to be attached.

Example
//Add a series of handlers in a control or behavior
$addHandlers(this.get_element(),{'mousedown' : this._
onMouseDown,'mouseup' : this._onMouseUp} , this);
_onMouseDown: function(e){...},
_onMouseUp: function(e){...}

clearHandlers() Method ($clearHandlers)

Static method that removes all the event handlers attached to a DOM element.

Parameters

element – the DOM element.

Remarks

This method should be used in the dispose() method of a component in order to remove the attached event handlers.

Example
//Clear all handlers attached to a element
$clearHandlers(this.get_element());

preventDefault() Method

Method that prevents the default event action from taking place.

Example
function myEvent(e)
{
  e.preventDefault();
}

removeHandler() Method

Static method that removes an event handler from the DOM element it is attached to.

Parameters

element – the DOM element.

eventName – the name of the event the handler is being removed from.

handler – the handler to be removed from the DOM element’s event.

Example
//Remove an event handler
Sys.UI.DomEvent.removeHandler(this.get_element(),'mousedown',handler);

stopPropagation() Method

Method that prevents the event from bubbling up the hierarchy.

Example
function myEvent(e)
{
  e.stopPropagation();
}

Sys.UI.MouseButton Enumeration

Sys.UI.MouseButton (Figure A-40) contains elements that represent the mouse buttons.

Figure A-40 Sys.UI.MouseButton

Sys.UI.Point Class

Sys.UI.Point (Figure A-41) contains information about a point’s position.

Figure A-41 Sys.UI.Point

Sys.UI.VisibilityMode Enumeration

Sys.UI.VisibilityMode (Figure A-42) contains two values for the visibility CSS property of a DOM element.

Figure A-42 Sys.UI.VisibilityMode