ASP.NET IronPython Resource

Learn python and .net

Beginning IronPython - Creating a basic ASP.NET Form (Part 1)

Ever wanted to know how to create an IronPython site but didn't know where to start? Here is a simple tutorial to get you up and running quickly.

Click for screencast.

If you haven't already, install the IronPython futures from http://www.asp.net/IronPython/.

IronPython sites can be created using Visual Studio 2005, 2008, or Visual Web Developer 2008. If you use Visual Studio 2005, you won't have debugging support.

Create a new site, and select “IronPython” as the type.

 

 

You will see a few default folders – a bin folder with the ironpython dll references, an app_script folder (analgous to the add_code folder in c#/vb) and a default.aspx file. In Ironpython, the code behind files end with a .py extension. Open the Default.aspx.py code behind. It has just the following code:

def Page_Load(sender, e):
    pass

As you can see it’s pretty blank. We will need to add the namespace references that are in a default c# web form. Replace the contents of this file with the following:

import clr
clr.AddReference('System.Data')
clr.AddReference('System.Configuration')
clr.AddReference('System.Web')
from System.Data import *
from System.Configuration import *
from System.Web import *
from System.Web.Security import *
from System.Web.UI import *
from System.Web.UI.WebControls import *
from System.Web.UI.WebControls.WebParts import *
from System.Web.UI.HtmlControls import *
 
def Page_Load(sender, e):
    if not sender.IsPostBack:
      pass
 

 

The syntax is a little different than your usual c# webform. The biggest difference is you need to add clr references to your main namespaces. This is to tell python to use the asp.net framework. Once those are added, the sub namespaces can then be referenced. Also, I’ve added the “Page.IsPostback” logic that is used in most webforms. The “pass” keyword is used when a method is blank. It is analogous to having { } in c# with no content.

Next we will create your honorary first “Hello world” form. It will be programmed so when a button is clicked, it will change the label to “Hello World”.

 

Go to the Default.aspx code-front file and add a Label and a Button. If you double click on the button, you will notice that it does not create an event handler automatically as it would in c#. This is a known issue in IronPython is is supposed to be fixed in the future. Go to the source view and find the code for the button. Add an event definition for the button: OnClick="Button1_OnClick". Your aspx file should now look like this:

<%@ Page Language="IronPython" CodeFile="Default.aspx.py"

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
&nbsp;<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_OnClick" />
   
    </div>
    </form>
</body>
</html>
 

Go to the code behind and add the event handler.

def Button1_OnClick(sender, e):
    Label1.Text = "Hello World"

You will notice that there aren't any return arguments and the parameters do not have types defined as in c#. This is a feature many dynamic languages. Variables aren't "typed". Now, open the project in the browser by hitting F5. Once the browser has opened, click on the button. The text should now change to “Hello World”.

Click below for full source code:
IronPythonBasicForm.zip (799.00 kb)

Posted: Aug 03 2008, 05:20 by Administrator | Comments (6) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: IronPython ASP.NET
Social Bookmarks: E-mail | Kick it! | DZone it! | del.icio.us

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading