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"  </script>    <html>  <body>  <h2>ASP.NET Simple Web Forms Demo</h2>    <form method="post" runat="server">   button: <asp:button id="mybutton" runat="server" Text="Click Me"            OnClick="mybutton_Click"/>  </form>    </body>  </html>

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′:

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:

  • MSN Introduction to ASP.NET and WebForms
  • ASP.NET Webforms
  • ASP.NET Trace
  • Share this:
  • Print
  • Email
  • Press This

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