AJAX Quickstarts   |   Silverlight Quickstarts   |   Dynamic Data Controls Quickstarts   |   Services Quickstarts   |   Dynamic Languages Quickstarts   |   ASP.NET AJAX Home   |      |  I want my samples in...     

ASP.NET Futures (July 2007): Using Dynamic Languages with ASP.NET

This walkthrough provides you with an introduction to dynamic languages for ASP.NET. It guides you through creating a simple page in Microsoft Visual Studio, adding controls, and adding event handlers using dynamic languages.

Tasks illustrated in this walkthrough include:

  • Adding controls to the default page.

  • Adding event handlers in a separate code file, using dynamic languages.

  • Adding a second page with event-handling code in the page.

Prerequisites

In order to complete this walkthrough, you will need:

  • Microsoft Visual Studio 2005 or Visual Web Developer Express Edition.
  • The ASP.NET Futures release (May 2007 or later) installed on your computer. The .msi installer file includes a Visual Studio Content Installer (.vsi) for creating a blank ASP.NET Futures Web application in Visual Studio.

This walkthrough assumes that you have a general understanding of working in Visual Web Developer. For an introduction, see Walkthrough: Creating a Basic Page in Visual Web Developer.

Creating a Web Site

In this part of the walkthrough, you will create a Web site with a dynamic language as the default language.

To create a Web site with a default ASP.NET Web page

  1. In Visual Studio (or Visual Web Developer), in the File menu, click New Web Site. The New Web Site dialog box is displayed.
  2. In the Language list, select a dynamic language. For example, select IronPython to make IronPython the default language for the Web site.

    Note: You can use statically compiled languages in the same Web application by creating pages and components in different programming languages.

  3. Under Visual Studio installed templates, click ASP.NET Web Site.
  4. In the Location box, select the File System box, and then enter the name of the folder where you want to keep the pages of your Web site. For example, type the folder name C:\BasicWebSite.
  5. Click OK. Visual Studio creates the folder and a new page named Default.aspx.

Adding Controls to the Default Page

In this part of the walkthrough, you will add server controls to the page.

To add controls to the page

  1. Switch to Design view.
  2. In the Toolbox, from the Standard group, drag three controls onto the page: a TextBox control, a Button control, and a Label control.
  3. Put the insertion point above the TextBox control, and then type Enter your name: to create a caption for the text box.

Programming the Button Control

For this walkthrough, you will write code that reads the name that the user enters into the text box and then displays the name in the Label control.

To add a button event handler

  1. Right-click the page and click View Code to show the separate code file. For example, if you are using IronPython, the file is Default.aspx.py.

    The file contains a stub event handler for the Load event of the page.

    Note: In IronPython, pass is a placeholder that does nothing.

  2. Replace the stub event handler with the following code to set the label text when the page is initialized:

    		
    def Page_Load(sender, e):
        if not IsPostBack:
            Label1.Text = "...Your name here..."
    
    IronPython

  3. Add the following code to create an event handler for the button's Click event:

    		
    def Button1_Click(sender, e):
        Label1.Text = Textbox1.Text
    
    IronPython

    In this release, event handlers must be coded and bound manually. You cannot create them by double-clicking a control in Design view or by selecting an event in the Properties window.

    Because dynamic languages do not have typed parameters and variables, you do not need to know the type of the event argument object.

    Note: In this release, IntelliSense support for dynamically typed variables is limited. You can press CTRL+SPACE to get a list of code elements that are currently in scope.

  4. Switch to Default.aspx and go to Source view, and then bind the event handler by adding an OnClick attribute to the Button control markup, as shown in the following example:
    <form id="form1" runat="server">
    <div>
        Enter your name:<br />
        <asp:TextBox ID="TextBox1" runat="server">
        </asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button"
            OnClick="Button1_Click"/><br />
        <br />
        <asp:Label ID="Label1" runat="server" Text="Label">
        </asp:Label>
        <br />
    </div>
    </form>
    
  5. Press CTRL+F5 to run the page in the browser using the ASP.NET Development Server.
  6. Enter a name into the text box and click the button. The name you entered is displayed in the Label control. If the name does not appear, check the spelling of the event handler in the OnClick attribute.
  7. In the browser, optionally view the source of the page you are running.
  8. Close the browser.
Form Example in IronPython
Run Sample View Source
Form Example in Managed JScript
Run Sample View Source

Adding a Page Without a Separate Code File

In this part of the walkthrough, you will add a page that contains its own code.

To add a Web page with its own code

  1. In Solution Explorer, right-click the name of the Web site, and then click Add New Item.
  2. Under Visual Studio installed templates, click Web Form.
  3. In the Name box, type SecondWebPage.
  4. In the Language list, select a dynamic language such as IronPython.
  5. Clear the Place code in separate file check box to create a single-file page with the code and markup in the same page.
  6. Click Add. Visual Studio creates the new page and opens it in Source view.
  7. Switch to Design view.
  8. Add a TextBox control, a Button control, and a Label control, as you did for the default page.

Programming the Button Control

For this walkthrough, you will add dynamic language code in a script block.

To add a default button event handler

  1. Switch to Source view.
  2. Add the following code to initialize the label and to create an event handler for the button's Click event.

    		
    <script runat="server">
    def Page_Load(sender, e):
        if not IsPostBack:
            Label1.Text = "...Your name here..."
    
    def Button1_Click(sender, e):
        Label1.Text = Textbox1.Text
    </script>
    
    IronPython

  3. In the Button control, bind the event handler by adding the OnClick attribute, as you did previously in this walkthrough. The following example shows the markup.
    <asp:Button ID="Button1" runat="server" Text="Button"
        OnClick="Button1_Click"/><br />
    
  4. Press CTRL+F5 to run the page in the browser using the ASP.NET Development Server.

See Also

Introduction to Dynamic Languages for ASP.NET


This topic is Microsoft ASP.NET Futures pre-release documentation and is unsupported by Microsoft. Blank topics are included as placeholders and existing content is subject to change in future releases.