DCSIMG
Using Script# to learn ASP.NET Ajax - Doron's .NET Space

Using Script# to learn ASP.NET Ajax

One of the really great things about the ASP.NET Ajax Extensions library is its client-side capabilities. It allows you to write OOP-Style code with namespaces, classes, inheritance and the works. Still, learning to use it can be a bit annoying. You don't get the intellisense or documentation support as you would with C#, and all in all the Visual Studio javascript editor is nothing to tell the kids about.

Therefore, what I would have liked is to be able to write some code in C#, and see how it gets translated to javascript. Nikhil Kothari's Script# does exactly that. This is in fact a C# compiler that generates javascript instead of IL. Recently, he added support for an ASP.NET Ajax only mode, meaning the javascript that is emitted relies only on Microsoft's standard library (instead of Nikhil's library).

This is a great way to write and learn ASP.NET Ajax client script. You want to know how to use classes and namespaces in ASP.NET Ajax? Just write it in C# like this:

public class Person
{
private string firstName;
private string lastName;
private DateTime birthDate;

public Person()
{
this.BirthDate = new DateTime(1983,10,18);
}

public string FirstName
{
get { return firstName; }
set { firstName = value; }
}

public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public string Name
{
get { return FirstName + " " + LastName; }
}

public DateTime BirthDate
{
get { return birthDate; }
set { birthDate = value; }
}
protected virtual string GetIntroduction()
{
return string.Format("Hi, my name is: {0} ", Name);
}
public void SayHi()
{
Script.Alert( GetIntroduction());
}
}

And see how it gets translated to this:

TestScriptSharp.Person = function TestScriptSharp_Person() {
this.set_birthDate(new Date(1983, 10, 18));
}
TestScriptSharp.Person.prototype
= {
_firstName:
null,
_lastName:
null,
_birthDate:
null,

get_firstName:
function TestScriptSharp_Person$get_firstName() {
return this._firstName;
},
set_firstName:
function TestScriptSharp_Person$set_firstName(value) {
this._firstName = value;
return value;
},

get_lastName:
function TestScriptSharp_Person$get_lastName() {
return this._lastName;
},
set_lastName:
function TestScriptSharp_Person$set_lastName(value) {
this._lastName = value;
return value;
},

get_name:
function TestScriptSharp_Person$get_name() {
return this.get_firstName() + ' ' + this.get_lastName();
},

get_birthDate:
function TestScriptSharp_Person$get_birthDate() {
return this._birthDate;
},
set_birthDate:
function TestScriptSharp_Person$set_birthDate(value) {
this._birthDate = value;
return value;
},
getIntroduction:
function TestScriptSharp_Person$getIntroduction() {
return String.format('Hi, my name is: {0} ', this.get_name());
},
sayHi:
function TestScriptSharp_Person$sayHi() {
alert(
this.getIntroduction());
}
}

Nice and simple, and even uses the javascript naming conventions for us :-). Now we can easily use this script from any aspx file by referencing it with a ScriptManager control.

<asp:ScriptManager ID="ScriptManager1" runat="server"> <Scripts> <asp:ScriptReference Path="~/App_Scripts/TestScriptSharp.js" /> </Scripts> </asp:ScriptManager> <script language="javascript"> var person = new TestScriptSharp.Person(); person.set_firstName("Doron"); person.set_lastName("Yaacoby"); person.sayHi(); </script>

In fact, you don't have to limit your usage of Script# for educational purposes only. The next time I'll have to write lot's of client-side code which uses the MicrosoftAjax library, I will probably use Script# for this. It contains most if not all the functionality javascript provides, and it gives me C#-comfort.

To get started with Script# I recommend watching the 10-minute screen-cast in Nikhil's post. Basically you have to download the MSI and create a visual-studio project using the built-in Script# templates.

A few warnings:

  1. You probably want to turn ReSharper off when you're using Script#, they don't play very well together.
  2. If you're using the "Atlas Class Library" project, make sure you remove any reference to an assembly that starts with ss (such as sscorlib.dll). Those are the assemblies for Nikhil's own script library (which is actually more powerful than the ASP.NET Ajax one, but it is not the standard). The assemblies you need start with aa (mainly aacorlib.dll).
  3. It took me a while to find the full documentation for Script#. It can be found here.

Happy scripting!

Published Saturday, June 16, 2007 7:28 AM by dorony
תגים:, ,

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: