Classes and ASP.NET AJAX - Enumerations
(Page 5 of 5 )
Another .NET type that is emulated by ASP.NET AJAX for JavaScript is Enum. You can create a custom enumeration using the createEnum() method. The API for this changed quite a bit during the Atlas and ASP.NET AJAX development cycle. In its current form, you can create an enumeration as shown in the following listing, but you cannot iterate over it. You can create a namespace, if you wish to use one:
Type.registerNamespace("ORA.MyEnums");
Then, create the enum object, assigning it an (empty) function:
ORA.MyEnums.Ajax = function() {};
Next, define all values in the enumeration, using the syntax below:
ORA.MyEnums.Ajax.prototype = {
"Asynchronous": 0,
"JavaScript": 1,
"and": 2,
"XML": 3
};
Finally, the enumeration needs to be registered:
ORA.MyEnums.Ajax.registerEnum("ORA.MyEnums.Ajax");
Example 4-6 shows a complete example that creates the enumeration and then accesses it.
Example 4-6. Using an ASP.NET AJAX Enum
ClientEnum.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 id="Head1" runat="server">
<title>ASP.NET AJAX</title>
<script language="Javascript" type="text/javascript">
function pageLoad() {
Type.registerNamespace("ORA.MyEnums");
ORA.MyEnums.Ajax = function() {};
ORA.MyEnums.Ajax.prototype = {
"Asynchronous": 0,
"JavaScript": 1,
"and": 2,
"XML": 3
};
ORA.MyEnums.Ajax.registerEnum("ORA.MyEnums.Ajax");
document.getElementById("output").innerHTML +=
ORA.MyEnums.Ajax.Asynchronous + " " +
ORA.MyEnums.Ajax.JavaScript + " " +
ORA.MyEnums.Ajax.and + " " +
ORA.MyEnums.Ajax.XML;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div id="output"></div>
</form>
</body>
</html>
This code outputs the string"0 1 2 3 "(the keys for the enumeration entries) in the<div>element.
Enumerations are also used internally by ASP.NET AJAX to define mouse button values (the following code snippet has been edited and reformatted for clarity).
Sys.UI.MouseButton = function() { };
Sys.UI.MouseButton.prototype = {
leftButton:0,
middleButton:1,
rightButton:2
};
Sys.UI.MouseButton.registerEnum("Sys.UI.MouseButton");
Summary
The ASP.NET AJAX client script library implements several convenient features not present in standard JavaScript, including OOP-like functionality and client-side equivalents of .NET Framework features. These features can be used by any JavaScript programmer, without repercussions to ASP.NET or the server-side features of ASP.NET AJAX.
For Further Reading
http://www.kevlindev.com/tutorials/javascript/inheritance
Online tutorial for JavaScript’s OOP capabilities
http://aspnetresources.com/blog/ms_ajax_cheat_sheets_batch2.aspx
“Cheat sheets” for ASP.NET AJAX’s JavaScript
extensions
http://ajax.asp.net/docs/ClientReference/Global/default.aspx
Documentation for helper functions and JavaScript
base type extensions
http://quickstarts.asp.net/Futures/ajax/doc/cssselectors.aspx
The ASP.NET AJAX Futures provides JavaScript helper
functions to select elements based on CSS rules
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter four of Programming ASP.NET AJAX, written by Christian Wenz (O'Reilly, 2007; ISBN: 0596514247). Check it out today at your favorite bookstore. Buy this book now.
|
|