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 Shared Code with Dynamic Languages for ASP.NET

Introduction

In this walkthrough, you will create a simple class with a dynamic language and then use it in an ASP.NET Web 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 Studio. For an introduction, see Walkthrough: Creating a Basic Page in Visual Web Developer.

Creating the Web Site and Page

If you have already created a Web site in Visual Studio (for example, by working with the companion walkthrough Creating a Basic Web Page with Dynamic Languages ), you can use that Web site and go to the next section. Otherwise, create a new Web site and page by following these steps.

To create a file system Web site

  1. In Visual Studio, in the File menu, click New Web Site. The New Web Site dialog box is displayed.

  2. In the Language list, click a dynamic language such as IronPython to make it 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:\SampleSite.
  5. Click OK. Visual Studio creates the folder and a new page named Default.aspx.

Creating a Shared Class

You can create reusable classes by keeping them in a folder named App_Script. The App_Script folder is created automatically when you create a new Web site using a dynamic language as the default language.

Note  You should put only classes (and other supported shared types) into the App_Script folder. Do not put pages, Web user controls, or other files that contain non-code elements into the App_Script folder.

To create a shared class in the App_Script folder

  1. In Solution Explorer, right-click App_Script, and then click Add New Item.

    Note  Be sure to create the new item in the App_Script folder, not in the root folder of the Web site.

  2. Under Visual Studio installed templates, click the module type for the dynamic language you selected as the default for the Web site. For example, if you selected IronPython as the default, click IronPython Module.
  3. In the Name box, type SampleModule.
  4. Click Add.

    Visual Studio opens the new module file in the editor. The module has nothing in it but a comment.

  5. In IronPython, add a class named SampleClass with a single property named TestString by creating access methods and assigning them to the property name with the property function. In Managed JScript, add a constructor for a SampleClass object; the constructor takes an initial value for a property.

    Note  Managed JScript does not have classes. It is a prototype-based, object-based language; objects can be created from other objects or by using constructors. Inheritance trees are supported through prototyping.

    The following example shows what the code looks like.

    		
    class SampleClass:
        "Sample class with one property"
        def __init__(self):
            self.__testString = ""
        def SetTestString(self, value):
            self.__testString = value
        def GetTestString(self):
            return self.__testString
        TestString = property(GetTestString, SetTestString)
    
    IronPython

    Note  In Python a property is created by defining get and set methods and then using the built-in method property to map these property accessor methods to the property name (TestString). The get method is GetTestString and the set method is SetTestString. The name of the instance variable that is used to store the property value begins with a double underscore, which causes the variable name to be mangled, so that external callers cannot use that name to access the variable. The instance variable is initialized when an instance of the class is created. The mangled name is still accessible to external callers, because Python does not have a private access level, but the mangling prevents name conflicts if subclasses have variables of the same name.

Using the Shared Class

The next step is to use the shared class in an ASP.NET Web page. You can use the Default.aspx page that was created when you created the Web site.

To use the shared class

  1. Open or switch to the Default.aspx page, and then switch to Design view.

    Note  If you do not have a Default.aspx page, you can use another page or add a new page to the Web site.

  2. From the Standard tab in the toolbox, drag a TextBox control, Label control, and Button control onto the page.

    Note  For this walkthrough, the layout of the page is not important.

  3. Right-click the page and click View Code, and then add import statements for the new module using the following code:

    		
    from SampleModule import SampleClass
    
    IronPython

  4. Add an event handler for the Click event of the button, with the following code:

    		
    def Button1_Click(sender, args):
        sc = SampleClass()
        sc.TestString = TextBox1.Text
        Label1.Text = sc.TestString
    
    IronPython

    Note  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.

  5. Switch to Source view, and then bind the event handler to the Click event using the following markup.
    <asp:Button ID="Button1" runat="server" Text="Button"
        OnClick="Button1_Click"/><br />
  6. Press CTRL+F5 to run the page.
  7. When the page appears in the browser, enter something in the text box, and then click the button.

    The property of the sample class is set to the value of the TextBox control, and then displayed in the Label control.

See Also

Dynamic Languages for ASP.NET: Introduction
Walkthrough: Creating a Basic Page in Visual Web Developer


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.