The HTML <img>element represents an image on the page. TheSys.UI.Imageclass implements an Atlas version of a client-side image (represented in the DOM with theImageobject). In addition to the common methods listed earlier in this chapter, the AtlasImageclass supports the following methods:
get_alternateText() Retrieves the value of thealtattribute
set_alternateText() Changes the value of thealt attribute
get_height() Gets the height of the image
set_height() Sets the height of the image
get_width() Gets the width of the image
set_width() Sets the width of the image
get_imageURL() Retrieves the relative or absolute URL of the image (srcattribute)
set_imageURL() Changes the relative or absolute URL of the image (srcattribute)
Once again, standard DOM properties are encapsulated in a class. You don’t have to learn much JavaScript, just get accustomed to the methods that Atlas exposes. Example 4-3 shows you how to manipulate the empty<img>element on the page, which initially looks like this:
<img id="Image1" />
By default, the XHTML validation in Visual Studio will complain about missing attributes, but you will be using JavaScript code to set the requiredsrcandaltattributes.
Example 4-3. Using an Atlas Image control
ControlImage.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 pageLoad() { var image = new Sys.UI.Image($("Image1")); image.set_imageURL("atlaslogo.gif"); image.set_alternateText("Atlas logo"); } </script>
Figure 4-3 shows the result. For this example to work, you need the file atlaslogo.gif in the root directory of the web site; you will find the file in the code downloads for this book (http://www.oreilly.com/catalog/atlas).
Figure 4-3.The Image control; the Properties window shows the alternate text
In HTML, the <a>element is used to link to other pages and to documents, and it is also for bookmarks. In Atlas, hyperlinks are represented with theSys.UI.HyperLinkclass. This class implements theget_navigateURL()andset_navigateURL()methods for setting the link target (only the target URL, not the target frame or window). It also provides aclickevent, which can be acted upon. (Event handling is covered later in this chapter in the “Handling Control Events” section.)
In Example 4-4, an empty link (<a></a>) is created, and a link target is added dynamically. In the example, the link is the same Atlas logo image that you used in the preceding example.
It is not possible to directly set the text of the link. A link might not necessarily be a text link, but could also contain an image or another element. Therefore, the text of the link can be thought of as another object, and if you want to set the link text, you have to put another element (with ID) inside the link.
Example 4-4. Using an Atlas Link control
ControlHyperLink.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 pageLoad() { var link = new Sys.UI.HyperLink($("Link1")); link.set_navigateURL(http://atlas.asp.net/); var image = new Sys.UI.Image($("Image1")); image.set_imageURL("atlaslogo.gif"); image.set_alternateText("Atlas logo"); } </script>
HTML supports various kinds of buttons, e.g., <input type="submit">to submit a form,<input type="reset">to clear a form (reset it to its original state), and<input type="button">and<button>for a button with no predefined behavior that can be enriched with JavaScript. Atlas implements buttons (<input type="button">or<button>, i.e., buttons that cannot serve any purpose without JavaScript) withSys.UI.Button. The following methods are supported:
get_argument() Retrieves the argument sent along with the command when the button is clicked
set_argument() Sets the argument of the button
get_command() Retrieves the command sent when the button is clicked
set_command() Sets the command of the button
Checkboxes
HTML uses <input type="checkbox"> for checkboxes. A checkbox has only two states: checked or not checked. These states can be set using JavaScript, so Atlas provides this functionality as well. The set_checked()method can change the state of a checkbox (by providing a Boolean value), andget_ checked()retrieves the current state. The associated class isSys.UI.CheckBox.
Example 4-5 uses HTML to create a checkbox, and Atlas/JavaScript to set its checked state to true.
Example 4-5. Using an Atlas CheckBox control
ControlCheckBox.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 pageLoad() { var checkbox = new Sys.UI.CheckBox($("CheckBox1")); checkbox.set_checked(true); } </script>
HTML selection lists (<select>...</select>) can come in different forms: a drop-down list where the user has to click to see all list elements, or a selection list where some of the elements are already visible. Both types of lists
Figure 4-5. Atlas has checked the checkbox
are covered by Atlas with the Sys.UI.Selectclass. Unlike using JavaScript to work with a<select>element, the ability to set the individual values of the list’s elements is not provided by the Atlas classes.
If the data for the list exists in the form of a .NETDataTableobject, data binding is a possibility. Chapter 9 explains this approach.
But we can demonstrate theget_selectedValue()method, which determines thevalue attribute of the currently selected item in the list.
When sending the form to the server via HTTP GET or POST, it is not essential to set thevalueattribute, because the caption of the element (the text between<option>and</option>) is passed forvalue. However, a list item with novalueproperty is empty in JavaScript. Therefore, you should always set thevalueproperty for all list elements.
Since event handling isn’t covered until later in this chapter, in Example 4-6, thechange event of the list is not captured, and instead the state of the list is analyzed every second. This is done using thesetInterval()JavaScript function. This polling technique is used only for the sake of the example here; Chapter 5 will cover a much better way to keep two elements in sync, namely through the use of data binding.
function pageLoad() { window.setInterval( function() { //access the list and output its selected value }, 1000); }
Example 4-6 shows how to use Atlas to check for a current selection and display the selection value in a<span>element (Sys.UI.Label).
Example 4-6. Using an Atlas Select control
ControlSelect.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"> var label; var select;
function pageLoad() { label = new Sys.UI.Label($("Label1")); select = new Sys.UI.Select($("Select1"));
// Poll every second to determine whether a value has been selected. window.setInterval( function() { label.set_text(select.get_selectedValue()); }, 1000); } </script>
Figure 4-6. The selected value is written to the Label control
Usingget_selectedValue()may be convenient, but only for regular selection lists. If you are using<select multiple="multiple">, you only get thevalueof the first list element that is selected, not of all selected elements. To check all selected elements, you would need to use Java-Script code to loop through all the items individually, as shown in the following snippet:
var op = document.forms[0].elements["Select1"]. options; for (var i=0; i < op.length; i++) { if (op[i].selected) { //element is selected } else { //element is not selected } }
Please check back next week for the conclusion to this article.