Classes and ASP.NET AJAX - Client Versions of .NET Classes
(Page 4 of 5 )
In addition to adding OOP-like features for JavaScript coding, ASP.NET AJAX achieves two goals through client class implementions that are analogs of some .NET classes:
Functionality missing in JavaScript is provided as part of ASP.NET AJAX.
.NET developers with little JavaScript experience can use some familiar elements in their code.
In my opinion, this is one of the areas where upcoming ASP.NET AJAX versions will most certainly add more features, so the following list of classes is neither exhaustive nor final. Two useful features that are already available areSys.StringBuilderand enumerations.
Sys.StringBuilder
One of the new features introduced in .NET 1.0 that really improved performance was the introduction of the StringBuilder class. The downside, however, is that applications are usually full of code such as that illustrated below:
string s = "", t;
while (){
t = <value>;
s += t;
}
The problem lies in the statements += t, which is equivalent tos = s + t. Whenever this code is executed, a copy ofsand a copy oftare created in memory, concatenated, then saved back intos. However, it’s inefficient to create a copy ofsto achieve these results. Therefore,StringBuilderuses an optimized algorithm for string concatenation.
In JavaScript, this approach does not have any measurable effect on memory (in fact, the implementation seems to be a tick slower than the standard approach). Then again, performance is not as critical an issue for client script as it is for server code. Nevertheless, for consistency with your server coding techniques, you can rely on your knowledge of .NET coding techniques and useStringBuilder on the client. Example 4-5 puts the StringBuilder class to work. It concatenates some strings to build an HTML chessboard.
Example 4-5. Using an ASP.NET AJAX StringBuilder
ClientStringBuilder.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">
window.onload = function() {
var sb = new Sys.StringBuilder();
for (var i = 8; i >= 1; i--) {
for (var j = 97; j <= 104; j++) {
sb.append(String.fromCharCode(j));
sb.append(i);
sb.append(" ");
}
sb.appendLine();
sb.appendLine();
}
document.getElementById("output").innerHTML = "<pre>" +
sb.tostring() + "</pre>";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div id="output"></div>
</form>
</body>
</html>
The built-in JavaScript functionString.fromCharCode()converts an ASCII code to its associated character, so the innerforloop runs from"a"through"h". As Figure 4-3 reveals, the code in Example 4-7 creates a simple chessboard.

Figure 4-3. A chessboard (with some potential)
Next: Enumerations >>
More ASP.NET Articles
More By O'Reilly Media
|
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.
|
|