By Christian Wenz
Price: $34.99 USD
£24.99 GBP
Cover | Table of Contents | Colophon
http://www.google.com/webhp?complete=1&hl=en) shows how an Ajax-enabled page can suggest words as a user enters text (also known as autocompletion).
Another Ajax-style application is the Microsoft Virtual Earth
site (http://www.virtualearth.comhttp://www.google.com/webhp?complete=1&hl=en) shows how an Ajax-enabled page can suggest words as a user enters text (also known as autocompletion).
Another Ajax-style application is the Microsoft Virtual Earth
site (http://www.virtualearth.com), which you'll explore in Chapter 12.XMLHttpRequest object, which handles the requests from the client to the server. (Additional knowledge of XML
and XSLT
is a plus, but is not mandatory; we don't use them much in this book.)ScriptManager, which will be discussed later in this chapter (see "The ScriptManager Control"), and UpdatePanel, which is discussed in Chapter 1.UpdatePanel control, Microsoft has not yet indicated when or how Atlas might be packaged for final release. Currently, the ASP.NET team is releasing periodic preview versions of Atlas
and interacting with the community, and that's the technology that is described in this book. For the March preview release, the ASP.NET team included a go-live license,
which is permission to release applications that contain Atlas bits. The go-live license is not a guarantee that Atlas is frozen, or even that it won't change, but it does mean that Microsoft is OK with your creating live applications with the technologies you will read about here.http://msdn.microsoft.com/vstudio/express/vwd/download, where you will find a web installer that not only downloads and installs VWD (see Figure 1-1 for the installer), but also takes care of installing
the .NET Framework 2.0, if it is not already installed on your system. Follow the instructions at the site.http://msdn.microsoft.com/vstudio/express/support/install)
ScriptManager server control, which will be covered later in this chapter. Usually, you will want to use both the server and client components of Atlas, of course.XMLHttpRequest really work.XMLHttpRequest), and receives it back with some extra text. The new version of the name is then shown to the user. This sample just shows you how easy it can be to set up an application using the features of Atlas. A more detailed description of the inner workings and the usage of Atlas can be found in the chapters following; however, the description of this example is not as exhaustive as the information about the other samples in this book.
WebService.asmx
<%@ WebService Language="C#" Class="WebService" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://hauser-wenz.de/atlas/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService {
[WebMethod]
public string sayHello(string name) {
return "Hello " + name + ", says the server!";
}
}
WebService that provides a reference to the web service. Figure 1-11 shows the JavaScript that displays.ScriptManager, <atlas:ScriptManager>. This control takes care of loading the required JavaScript libraries of Atlas, depending on which browser is used.<atlas:ScriptManager> element has been replaced with this code (the undecipherable data in the URL will be a bit different on your system):<script src="/Atlas/WebResource.axd?d=EiZ5MhryFS7wRPgWKwT3L2TYwCkaaO5mtAO5KyVbAcNmREm0baC0S _edhhqj_Y6ZuRY56z97Nu5lD2Fw5ITB3mHpybsGTsINHdsdQ_BVGi7cUBG1EWW_cWGx-I8vZeWK0 &t=632781806040000000" type="text/javascript"></script> <script src="/Atlas/WebResource.axd?d=EiZ5MhryFS7wRPgWKwT3L2TYwCkaaO5mtAO5KyVbAcNmREm0baC0S _edhhqj_Y6ZuRY56z97Nu5lD2Fw5ITB3g4TEhGZLS-_Daibixpp8tw1&t=632781806040000000" type="text/javascript"></script> <script src="atlasglob.axd" type="text/javascript"></script>
<script> tag is used only in non-IE browsers to add a compatibility layer; the second one loads the Atlas core library. The third <script> element contains some client-specific culture information.ScriptManager element must be present on all pages that use Atlas features. It can also be used to load additional JavaScript libraries, either those that come with Atlas, or your own scripts:<atlas:ScriptManager ID="ScriptManager1" runat="server"> <Scripts> <atlas:ScriptReference Path="MyScript.js" /> </Scripts> </atlas:ScriptManager>
ScriptManager control, one of two key server controls that ship with Atlas. In the next chapter, you'll have a look at the JavaScript you need to work with Atlas.http://atlas.asp.net/docs/Server/Microsoft.Web.UI/ScriptManager/Declarative Syntax.aspx
ScriptManager controlhttp://msdn.microsoft.com/msdnmag/issues/06/07/AtlasAtLast/default.aspx
http://weblogs.asp.net/atlas
<script> element. You can also use the src attribute of the <script> element to point to the URL of an external script file to load.<script> tag on a web page. However, to satisfy W3C standards and the needs of less-used or older browsers (including outdated versions), it's always best to specify the language using the following syntax:<script language="JavaScript" type="text/javascript"> ... </script>
<input type="button" onclick="doSomething( );" />.javascript: pseudoprotocol, making it, for instance, easy to use JavaScript in hyperlinks.public, private, protected, etc.) can be implemented only in a limited way. Nevertheless, it is possible to create classes in JavaScript and even to provide rudimentary support for class inheritance.this keyword provides access to the properties of the current class, making it possible to set properties and define methods.book.title).JavaScript-class.htm <!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> <title>JavaScript</title> </head> <body> <script language="JavaScript" type="text/javascript"> function Book(isbn, author, title) { var _isbn = isbn; var _author = author; var _title = title; this.get_isbn = function() { return _isbn; } this.set_isbn = function(value) { _isbn = value; } this.get_author = function() { return _author; } this.set_author = function(value) { _author = value; } this.get_title = function() { return _title; } this.set_title = function(value) { _title = value; }
document object grants access to all elements on the current page. document is a representation of the DOM that is accessible to JavaScript. To make the access as convenient as possible, there are several subproperties that allow direct access to special page elements. Here are some examples, listed alphabetically:document.embeds
<embed>) on the current pagedocument.forms
<form> elements on the pagedocument.frames
document.images
document.links
document.forms, which lets you access all <form> elements on the current page, such as text boxes and buttons. Admittedly, there is usually only a single form on a page. However, the document.forms[0] property (the document.forms object and its friends or using document.getElementById() with the innerHTML property suffices. Yet there are some cases where access to the DOM itself is required. Appendix B contains a complete list of supported methods
for accessing the DOM. Here are some of the most important ones:getElementsByTagName(
name
)
createElement(
name
)
createAttribute(
name
)
createTextNode(
text
)
appendChild(
node
)
<span> element and a text node. In this example, the appendChild() method comes into play: first, the text child is added to the <span> element, and then the <span> element is added to the paragraph.http://www.nikhilk.net/Project.ScriptSharp.aspx
http://www.adaptivepath.com/publications/essays/archives/000385.php). However, only the term is new, not the technology itself. Although XML can be part of an Ajax application (but doesn't have to be!), and some CSS may also be in the mix, the foundation of any Ajax-powered application is JavaScript.XMLHttpRequest
XMLDocument
JavaScript Object Notation (JSON)
XMLHttpRequest object, which enables you to make HTTP requests (and get responses), without performing a full page postback and refresh.XMLHttpRequest object, which enables you to make HTTP requests (and get responses), without performing a full page postback and refresh.XMLHttpRequest and therefore is Ajax-compatible. According to a study conducted by Net Applications
(responseXML property of the XMLHttpRequest object expects the return value of the remote call to be in the form of an XMLDocument object. This requires the server code to return well-formed XML data so that the client script can parse it. However, it is easy to access this XML data; you have full DOM support for doing so.XMLDocument object. The following example shows how to use quite a lot of them.
<book title="Programming Atlas" author="Christian Wenz">
<chapters>
<chapter number="1" title="Introduction" />
<chapter number="2" title="JavaScript" />
<chapter number="3" title="Ajax" />
</chapters>
</book>
Content-type HTTP header of the response is explicitly set to "text/xml". If this header is omitted, some browsers (most notably, Mozilla and derivatives) refuse to parse the return data, and the responseXML object is set to null. The following C# code in an ASP.NET page shows how to set the content type appropriately:
void Page_Load()
{
if (Request.QueryString["sendData"] != null &&
Request.QueryString["sendData"] == "ok")
{
string xml = "<book title=\"Programming Atlas\"
author=\"Christian Wenz\"><chapters><chapter number=\"1\" title=\"Introduction\"
/><chapter number=\"2\" title=\"JavaScript\" /><chapter number=\"3\" title=\"Ajax\"
/></chapters></book>";
Response.ContentType = "text/xml"; Response.Write(xml);
Response.End();
}
}
http://www.json.org). With JSON, JavaScript objects or data can be persisted (serialized) in a short and easily understandable way, without requiring a lot of JavaScript code to either write or read the data. JSON makes use of an often-overlooked feature of JavaScript, or to be exact of the ECMAScript language specification, also known as ECMA-262.book object.
{"book": {
"title": "Programming Atlas",
"author": "Christian Wenz",
"chapters": {
"chapter": [
{"number": "1", "title": "Introduction"},
{"number": "2", "title": "JavaScript"},
{"number": "3", "title": "Ajax"}
]
}
}}
book property contains title, author, and chapters properties. The chapters property contains several chapter subelements, each with a number and a title property. This can be best visualized when looking at it as XML data. You will remember it as the exact same XML that was used in the previous section, "The XMLDocument Object," as shown in this code:
<book title="Programming Atlas" author="Christian Wenz">
<chapters>
<chapter number="1" title="Introduction" />
<chapter number="2" title="JavaScript" />
<chapter number="3" title="Ajax" />
</chapters>
</book>
json.html
<!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>
<title>JSON</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
var json = '{"book": { "title": "Programming Atlas", "author": "Christian Wenz",
"chapters": {"chapter": [ {"number": "1", "title": "Introduction"}, {"number": "2",
"title": "JavaScript"}, {"number": "3", "title": "Ajax"} ]} }}';
XMLHttpRequest object. You also learned how to process complex data returned by the server using JavaScript and either XML or JSON.http://www.adaptivepath.com/publications/essays/archives/000385.php
http://www.json.org
Ajax Hacks by Bruce W. Perry (O'Reilly)
Head Rush Ajax by Brett McLaughlin, Elisabeth Freeman (O'Reilly)
Sys.UI namespace. Sys.UI is the client-side equivalent of the similarly named and well-known Web.UI namespace in ASP.NET.Web.UI, as well.Sys.UI contains 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.Sys.UI namespace. Sys.UI is the client-side equivalent of the similarly named and well-known Web.UI namespace in ASP.NET.Web.UI, as well.Sys.UI contains 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.|
Atlas control
|
Description
|
HTML element
|
JavaScript equivalent
|
|---|---|---|---|
|
Sys.UI.Window
|
Implements JavaScript pop-up windows
|
N/A
|
window.alert(), window.confirm(), window.prompt()
|
|
Sys.UI.Label
|
Implements a span or label element
|
<span>, <label>
|
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.Sys.UI.Window. This implements a client-side message box.
The JavaScript language supports three types of modal message boxes:
window.alert()
window.confirm()
window.prompt()
Sys.UI.Window class, the functionality for calling window.alert() or window.confirm() is encapsulated in the messageBox() method. The default behavior is to present a window.alert() box. This corresponds to the message box style Sys.UI.MessageBoxStyle.OK. The alternative is to use the Sys.UI.MessageBoxStyle.OKCancel style, which uses window.confirm() under the covers.window.prompt() window? For consistency with Visual Basic, this is implemented via the inputBox() method instead of the messageBox() method.<input type="button" value="MessageBoxOK" onclick="MessageBoxOKClick();" /> <input type="button" value="MessageBoxOKCancel" onclick="MessageBoxOKCancelClick();" /> <input type="button" value="InputBox" onclick="InputBoxClick();" />
initialize() method of the element whose events
you want to handle. This enables all the mechanisms that are internally used to capture events. Then setting up events is a two-step process.<
element
>.<
event
name
>.add(<
method name
>).