Aspf Demo

With the release of version 1.1 F# can now define object oriented classes. This is extremely useful when working with ASP.NET as it allows the programmer to implement a code behind class in very much the same way they would in C# or VB.NET, they can then have most of the user interface defined inside ASPX file itself. I have updated this demo to do just that, and the source is available here. Thanks to Visual Studio 2005’s build in web server it is possible to run the demo on your local computer just by pressing F5, no more messing around configuring IIS.

This addition means it is now easy to implement Web Services too, taking full advantage of the .Net frameworks web services stack. I have implemented a very simple web service and the results can be seen here.

Let’s take a quick look at the “Hello User” example:

type Handler =
     class
         inherit Page
             val mutable OutputControl : Label
             val mutable InputControl : TextBox
             new() =
             {inherit Page();
             OutputControl = new Label();
             InputControl = new TextBox()}

         member x.SayHelloButton_Click((sender : obj), (e : EventArgs)) =
             x.OutputControl.Text <- ("Hello ... " ^ x.InputControl.Text)
     end

The class we define contains 2 mutable fields, one to hold a reference to the TextBox which will receive the input and one for the Label that will hold the output, these can be seen at the top of the class. The page also contains a Button but as we don’t interact with that programmatically there is no need for it to be defined. Next we must define a constructor, to keep F# relatively free of null pointers the constructor must initialise all of the classes fields, even though this will really be done by the class that is generated from the ASPX page in our case. The only other class member is the event handler “SayHelloButton_Click”, which handles the user clicking on the pages button. You will note the class contains no code to wire up event handler, this is because this is done in the .ASPX page by adding the event handler to the button definition.