ASP.NET Webforms and Event Handling

To create more active server pages as done in the first example, it is necessary to add code and reference elements of a page. The most simple way is to add a script section in the ‘index.aspx’ source. It may be surprising to see that the event handler is defined as a member function of a System.Web.UI.Page class definition.

In the next example we add an event handler for a button click. As usual you need a ‘web.config’ file and a subdirectory bin with with FSharp.Compiler.CodeDom.dll, see in the first example how to do it, the source file in our example should be named index.aspx:

<%@ Page Language="F#" Trace="false" %>

<script runat="server">
member page.mybutton_Click((sender:obj), (e:EventArgs)) =
 page.mybutton.Text <- "Clicked"

<h2>ASP.NET Simple Web Forms Demo</h2>

<form method="post" runat="server">
 button: <asp:button id="mybutton" runat="server" Text="Click Me"


When starting the web server xsp2 in the directory of your source file you should see the following content in your web browser when pointing it to ‘http://localhost:8080&#8217;:

When you press the button the text in the button should change to:

One useful feature which you may need in the future is tracing. Change the attribute ‘Trace’ in the first line of the source file from “false” to “true”:

<%@ Page Language="F#" Trace="true" %>

When loading the page it will include additional tracing and timing information:

Further reading:

This entry was posted in Getting Started, Web. Bookmark the permalink.

One Response to ASP.NET Webforms and Event Handling

  1. Pingback: F# Discoveries This Week 10/27/2010 « F# Central

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s