JavaScript Generation in ASP.NET.
Introduction
ASP.NET applicatinos use JavaScript to provide rich client side functionality.
Typically ASP.NET Controls register references to JavaScript files and generate
script instantiating JavaScript components. Usually script generation is implemented
using string formatting tools such as StringBuilder or String.Format. This way
leads to brittle and error prone code even for simple components. If components
are complex and should be combined with each other, problem becomes just unsolvable.
LiveUI provides tools to generate JavaScript in object oriented way and utility
controls for ASP.NET applications. This document describes how JavaScript generation
works and how to use it in ASP.NET applications.
JavaScript generation basics
Central class in LiveUI generation engine is Js. Js builds JavaScript object
model which can be rendered to text. Principially, JavaScript object model
is a hierarchical structure of special objects representing function calls,
variable declarations and so on... Js helps to build this structure in a natural
C# friendly way. For example, there is C# code generating function call
- var js =
new
Js();
- js.Call("alert", js.Const("Hello
world"));
This code produces javaScript:
In this case following JavaScript model is created internally:

Important feature of JavaScrript generation is that it declares variables
automatically. For example, if "Hello world" constant were used twice, new variable
v0 would be declared. So, that C# code
- var helloWorld = js.Const("Hello
world");
- js.Call("alert", helloWorld);
- js.Call("alert", helloWorld);
generates JavaScript
- v0 = 'Hello world';
- alert(v0);
- alert(v0);
There are commonly used Js methods and samples
| C# |
JavaScript |
|
|
|
var menu = js.New("Menu");
|
|
js.Array(js.Const(1), js.Const(2));
|
|
js.Json(js.JsonItem("name",
"Alex"));
|
|
js.Native("window.open({0})",
js.Const("http://url.com"));
|
window.open('http://url.com')
|
And that's how to render js to text:
- var js =
new
Js();
- js.Native("window.open({0})",
js.Const("http://url.com"));
- js.PrepareRender();
- string
result = js.Render();
Using JavaScript generation in ASP.NET Pages
LiveUI provides JsManager control for using Js class in
ASP.NET pages. Typically JsManager is placed on ASP.NET page and new Js instance is created for each request. At the end of request
processing pipeline Page and controls are rendered to Html and Js is
rendered to JavaScript

There is a sample of simplest page using JavaScript generation:
- <%@
Page
Language="C#"
AutoEventWireup="true"%>
- <%@
Register
Assembly="Xtensive.Web"
Namespace="Xtensive.Web.Controls"
TagPrefix="liveui"
%>
- <%@
Register
Assembly="System.Web.Extensions,..."
TagPrefix="asp"
%>
- <html>
- <head
runat="server"
/>
- <body>
- <div>
- <form
runat="server">
- <asp:ScriptManager
ID="scriptManager"
runat="server"
ScriptMode="Debug"
/>
- <liveui:JsManager
runat="server"
/>
- </form>
- </div>
- </body>
- </html>
- public
partial
class
WebForm1 : System.Web.UI.Page
- {
- protected
void Page_Load(object
sender, EventArgs e)
- {
- var js =
JsManager.GetCurrent(this).Js;
- js.Call("alert",
js.Const("Hello world!"));
- }
- }
And there is a result

|