Atlas Client Controls

In this first article of a three-part series, you'll learn about the client-side controls that ship with Atlas. These behave very similar to ASP.NET web controls. This article is excerpted from chapter four of the book Programming Atlas, written by Christian Wenz (O'Reilly, 2006; ISBN: 0596526725). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
April 19, 2007
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

In Chapters 2 and 3, you learned the basics of JavaScript and key Ajax technologies, especially asynchronous calls, that support it. As you saw, Ajax itself is no big deal. The effects that Atlas lets you create are possible without Atlas. Everything you do with Atlas creates HTML, CSS, and JavaScript on the client side, which is also possible with any other server-side technology.

The real value of Atlas is that it greatly facilitates development of Ajax-powered applications. Although you can create applications without it, Atlas can make their implementation go faster. Also, with Atlas, your need to master browser-agnostic JavaScript is not a top priority, though, as is always the case, having such skills will give you a much better understanding of how Atlas works its magic.

This chapter covers client-side controls that ship with Atlas and mimic the behavior of ASP.NET web controls. This not only allows for consistent development on both the server and the client, but also supports convenient features like data binding, which you’ll explore in Chapter 5.

Introducing Atlas Client Controls

Atlas implements its client controls in the Sys.UInamespace.Sys.UIis the client-side equivalent of the similarly named and well-knownWeb.UInamespace in ASP.NET.

In older Atlas releases, the client-side namespace was namedWeb.UI, as well.

Sys.UIcontains a large number of Atlas HTML controls and web controls. The functionality of Atlas controls is similar but not identical to ASP.NET server controls. Atlas controls provide a consistent, browser-independent model that enables JavaScript code to access and change client controls properties, something that with non-Atlas controls would require quite a bit of JavaScript knowledge as well as some workarounds for browser inconsistencies.

Table 4-1 lists controls provided by Atlas. The table lists the HTML elements that the Atlas control works with and the equivalent DOM object or method that you would use in JavaScript.

Table 4-1. Atlas controls

 

Atlas control

Description

HTML element

JavaScript equivalent

Sys.UI.Window

Implements JavaScript pop-yp windows

N/A

window.alert(),  window.confirm(), window.prompt( ) 

Sys.UI.Label

Implements a span or label element

<span>,<label>

label

Sys.UI.Image

Implements an image

<img>

image

Sys.UI.HyperLink

Implements a link

<a href="...">

link

Sys.UI.Button

Implements a button

<input type="button">, <input type="submit">, <input type="reset">, <button>

button,submit, reset

Sys.UI.CheckBox

Implements a checkbox

<input 
type="checkbox">

checkbox

Sys.UI.Select

Implements a selection list

<select>

select

Sys.UI.TextBox

Implements a text field

<input type="text">, <input 
type="password">, <textarea> 

text,password, textarea

Using Atlas Controls

There are two concepts the Atlas framework uses with the controls in Sys.UI. Some of these controls provide JavaScript access to standard JavaScript methods. The others provide JavaScript access to HTML elements on the current page. Both ways are demonstrated in this section.

Accessing JavaScript Methods

One example of the first method is implemented by Sys.UI.Window. This implements a client-side message box. The JavaScript language supports three types of modal message boxes:

window.alert()
   Message box with an OK button

window.confirm()
   Message box with OK/Cancel or Yes/No buttons

window.prompt()
   Message box with an input field and an OK button

Inside the AtlasSys.UI.Windowclass, the functionality for callingwindow.alert()orwindow.confirm()is encapsulated in themessageBox()method. The default behavior is to present awindow.alert()box. This corresponds to the message box styleSys.UI.MessageBoxStyle.OK. The alternative is to use theSys.UI.MessageBoxStyle.OKCancelstyle, which useswindow.confirm()under the covers.

But what about thewindow.prompt()window? For consistency with Visual Basic, this is implemented via theinputBox()method instead of themessageBox()method.

The followingexample implements all three variants of client modal window. Three client-side buttons are in place for calling the Atlas functionality:

  <input type="button" value="MessageBoxOK" onclick="MessageBoxOKClick();" />
  <input type="button" value="MessageBoxOKCancel"
  onclick="MessageBoxOKCancelClick();" />
  <input type="button" value="InputBox" onclick="InputBoxClick();" />

Each of the three functions—click1(),click2(), andclick3()—call an Atlas method, as shown in the following code:

  <script language="JavaScript" type="text/javascript">
  function MessageBoxOKClick() {
    Sys.UI.Window.messageBox("Using Sys.UI.Window");
  }

  function MessageBoxOKCancelClick() {
   
Sys.UI.Window.messageBox("Using Sys.UI.Window", Sys.UI.MessageBoxStyle.  
  OKCancel);
  }

  function InputBoxClick() {
   
Sys.UI.Window.inputBox("Using Sys.UI.Window", "<enter text here>");
  }
  </script>

To use Atlas functionality in a page, you must include the Atlas library. The AtlasScriptManager element takes care of that:

  <atlas:ScriptManager runat="server"></atlas:ScriptManager>

Example 4-1 shows the code you need for your first Atlas example in this chapter.

Example 4-1. Modal JavaScript windows with Atlas

ControlMessageBox.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">
 
<title>Atlas</title>

  <script language="JavaScript" type="text/javascript">
  function MessageBoxOKClick() {
    Sys.UI.Window.messageBox("Using Sys.UI.Window");
  }

  function MessageBoxOKCancelClick() {
    Sys.UI.Window.messageBox("Using Sys.UI.Window", Sys.UI.MessageBoxStyle. OKCancel);
  }

  function InputBoxClick() {
   
Sys.UI.Window.inputBox("Using Sys.UI.Window", "<enter text here>");
  }
  </script>

</head>
<body>
 
<form id="form1" runat="server">
   
<atlas:ScriptManager runat="server">
   
</atlas:ScriptManager>
   
<div>
     
<input type="button" value="MessageBoxOK" onclick="MessageBoxOKClick();" /
>
      <input type="button" value="MessageBoxOKCancel" onclick="MessageBoxOKCancelClick();" />
      <input type="button" value="InputBox" onclick="InputBoxClick();" />
    </div>
 
</form>
</body>
</html>

Figure 4-1 shows the result when you click the InputBox button.


Figure 4-1.  Clicking a button opens a JavaScript window

This is nice functionality, but not yet of real value, since only very basic JavaScript functionality is encapsulated by the Atlas controls you are using. However, there are other controls with actual real-world use.

Accessing HTML Elements

Atlas controls also enable you to put HTML controls in the page and then access them usingan object-oriented, client-side approach. So although you are using HTML elements, you can use a client-side abstraction layer to access their contents.

The syntax for using Atlas to access HTML elements can seem a bit strange at first. Imagine the page contains a<span>element like the following:

  <span id="Label1">This is a label</span>

You could also use a<label>HTML element; however, this is most commonly used as a caption element for form fields, although the output in the browser is identical to a<span>element.

If you were using plain old JavaScript, you could access this element with the following code:

  var label = document.getElementById("Label1")

You could then set some properties of this element, including style information. However, the exact JavaScript you use would be different on different browsers, and you have to have a fairly good knowledge of JavaScript and the DOM, beyond just mastering the syntax.

The Atlas way is different. First, you have to determine the appropriate Atlas control class for the client-side element. (These are listed in Table 4-1.) For the<span>element, you useSys.UI.Label. The code must instantiate the class and provide the ID of the HTML element. However this ID will be specified in a nonstandard way: startingwith a dollar sign, and with the actual ID in parentheses:

  var label = new Sys.UI.Label($("Label1"));

The next step is not mandatory in this specific example, but it is in most other scenarios and is therefore generally recommended: calling theinitialize()method. This method registers delegates and event handlers so that you can set such properties. If you do not use event handling(as in the next few examples), calling theinitialize()method can be skipped:

  label.initialize();

Finally, you can call the methods that get and set the properties that these elements provide. Table 4-2 lists the most commonly used class members. More information about these common methods is provided later in the chapter. In addition, the sections that follow describe methods that are unique to each Atlas control class.

Table 4-2. Standard Atlas methods for setting HTML element properties

Method Description
get_accessKey()

Retrieves the access key of an element

set_accessKey()

Sets the access key of an element

get_cssClass()

Retrieves the CSS class of an element

set_cssClass()

Sets the CSS class of an element (overwriting existing CSS classes) 

addCssClass()

Adds a CSS class to an element (leaving existing CSS classes intact)

containsCssClass()

Checks whether an element has a certain CSS class

removeCssClass()

Removes a specific CSS class from an element

toggleCssClass()

Adds the class to an element if it is not already there, otherwise, it removes the class

get_enabled()

Retrieves whether an element is enabled (true) or not (false)

set_enabled()

Enable (true) or disable (false) an element

get_tabIndex()

Retrieves the tab index of an element

set_tabIndex()

Sets the tab index of an element

get_visibilityMode()

Retrieves thevisibilityCSS style of an element

set_visibilityMode()

Sets thevisibilityCSS style of an element

Table 4-2. Standard Atlas methods for setting HTML element properties (continued)

Method Description
get_visible()

Retrieves whether an element is visible (true) or not (false)

set_visible()

Makes an element visible (true) or invisible (false)

focus()

Sets the focus to an element

scrollIntoView()

Scrolls the current document to the position of an element

getLocation()

Retrieves the coordinates of an element

setLocation()

Sets the coordinates of an element

As you can see, a lot of the relevant style information about an element on a page can be set using the abstraction layer Atlas is providing.

Labels

For the Atlas Label control, Atlas supports the two following additional methods, which are both shown in Example 4-2.

get_text()
   Retrieves the current text of the element

set_text()
   Sets (changes) the text in the element

Remember that JavaScript and the browser DOM do not offer an equivalent to ASP.NET’sInnerTextproperty. The property that bothget_text()andset_text()are accessing isinnerHTML, so you always have to consider escaping special characters to avoid side effects. This will be covered in Chapter 5.

Example 4-2 does three things:

  1. It creates the client-sideSys.UI.Labelobject.
  2. It reads the old text using theget_text()method.
  3. It writes new text using theset_text()method.

Example 4-2. Using an Atlas Label control

ControlLabel.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
  <title>Atlas</title>

  <script language="JavaScript" type="text/javascript">
 
window.onload = function(){
    var label = new Sys.UI.Label($("Label1"));
    var d = new Date();
    var time = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
    label.set_text(label.get_text() + time);
 
}
  </script>

</head>
<body>

  <form id="form1" runat="server">
    <atlas:ScriptManager runat="server">
    </atlas:ScriptManager>
    <div>
     
<span id="Label1">time goes here: </span>
    </div>
  </form>
</body>
</html>

After the page loads, the current time is determined and then put in the<span>element. Figure 4-2 shows the result.


Figure 4-2.  The current time appears in the label

In ordinary JavaScript programming, you setwindow.onloadto an anonymous JavaScript function to be sure that code will be executed only when the HTML markup in the page has been fully loaded. To simplify this, Atlas comes with a convenient feature: if you call an Atlas function namedpageLoad(), the function will execute when the page’sloadevent occurs, first guaranteeing that the Atlas framework has been completely loaded and initialized.

Please check back next week for the continuation of this article.

blog comments powered by Disqus
ASP.NET ARTICLES

- Implementing ASP.NET 4.0 Page.MetaDescriptio...
- ASP.Net Development Tips
- Intro to Sessions in ASP.Net
- Google Maps API Introduction in ASP.NET usin...
- Creating an ASP.NET 3.5 Gridview Image Galle...
- Encrypt QueryString in ASP.NET 3.5 using VB....
- ASP.NET 3.5 Drop Down List Controls
- Connect to Access Database with ASP.Net
- Secure Audio Streaming with ASP.Net and Flash
- Dynamic Sitemap and Navigation in ASP.Net
- Implement Gzip and Deflate Compression in AS...
- Run ASP.Net in Ubuntu with Apache
- ASP.Net Mono Website Contact Forms
- ASP.Net URL Rewriting Methods
- Murach`s ASP.NET 4 Web Programming with C# 2...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials