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 2008 or Visual Web Developer 2008 Express Edition.
- A copy of the website included in the ASP.NET Dynamic Language Support download. There is currently no project template, so it is necessary to copy the website in order to start with a blank ASP.NET Dynamic Language website.
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
- Copy the files from the ASP.NET Dynamic Language Support project into an empty directory.
- In Visual Studio (or Visual Web Developer), in the File menu, click Open Web Site. The Open Web Site dialog box is displayed.
- Select the directory in which you copied the files in step 1. Make sure that FileSystem is selected in the left panel of the dialog.
Note: You can use statically compiled languages in the same Web application by creating pages and components in different programming languages.
- Click Open. Visual Studio opens the folder as a website and displays the files in the Solution Explorer.
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
- Switch to Design view.
- In the Toolbox, from the Standard group, drag three controls onto the page: a TextBox control, a Button control, and a Label control.
- 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
- 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.
- Replace the stub event handler with the following code to set the label text when the page is initialized:
IronPython
def Page_Load(sender, e): if not IsPostBack: Label1.Text = "...Your name here..."
- Add the following code to create an event handler for the button's
Clickevent:IronPython
def Button1_Click(sender, e): Label1.Text = Textbox1.Text
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.
- Switch to Default.aspx and go to Source view, and then bind the event handler by adding an
OnClickattribute 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>
- Press CTRL+F5 to run the page in the browser using the ASP.NET Development Server.
- 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
OnClickattribute. - In the browser, optionally view the source of the page you are running.
- Close the browser.
Programming the Button Control
For this walkthrough, you will add dynamic language code in a script block.
To add a default button event handler
- Switch to Source view.
- Add the following code to initialize the label and to create an event handler for the button's Click event.
IronPython
<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>
- In the Button control, bind the event handler by adding the
OnClickattribute, 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 />
- Press CTRL+F5 to run the page in the browser using the ASP.NET Development Server.
Using Access Data on an ASP.NET Web Page
You can now use the database in a Web page. This part of the walkthrough uses an AccessDataSource control and a DataList control.
To add AccessDataSource and DataList controls to the page
- Open the Default.aspx page (or another page that you want to use) and switch to Design view.
- From the Data group in the toolbox, drag an AccessDataSource control onto the page.
Note If the Access Data Source Tasks menu does not appear, right-click the control and then click Show Smart Tag.
- On the Access Data Source Tasks shortcut menu, click Configure Data Source. The Configure Data Source wizard is displayed.
- On the Choose a database page, in the Microsoft Access Data file box, type ~/App_Data/Northwind.mdb or use the Browse button to select the .mdb file.
- Click Next to open the Configure Select Statement page, and then click Specify columns from a table or view.
- In the Name list, click Categories.
- Select the CategoryName and Description check boxes and then click Next.
- Optionally, click Test Query to test your query.
- Click Finish.
- From the Data group in the toolbox, drag a DataList control onto the page.
- On the DataList Tasks menu, in the Choose Data Source box, click AccessDataSource1.
- Click Ctrl+F5 to run the page with the default layout.
- Close the browser.
Dynamic Data Samples for Preview 4
This is the primary project sample that shows most of the new functionality that has been added to Dynamic Data since .NET 3.5 SP1. The default.aspx page highlights many of the new items that are being added in .NET 4, in particular:
• Filter Templates. Filters are now first class citizens in Dynamic Data. There is a new FilterTemplate directory that contains the default filter templates and supports user defined filter templates. Filters can be applied to columns using the new FilterUIHint attribute.
• Entity Templates – The Details, Edit and Insert page templates in version 1.0 forced a two column display style (field name, field value). Entity templates allow for the layout of an entity to be arbitrarily customized.
• Field Templates. The following new field templates have been created.
• Email Field Template. Data fields in the model that are marked with DataType(DataType.Email) will be displayed as mailto: hyperlinks that will launch the email client when clicked.
• Url Field Template. Fields in the model that are marked with DataType(DataType.Url) will be displayed as hyperlinks that will open a new window with the given URL.
• Many to Many Relationships in Tables. Entity Framework models support many to many relationships. Dynamic Data will display these as a list of values or in edit mode a list of checkboxes for the selectable columns.
• Enumeration on Model. If a column in the model is associated with an enumeration data type it will be displayed as a dropdown list of the values from the enumeration.
• Enumeration using Metadata. If a column in the model has an EnumDataType(typeof(enum)) attribute it associated with an enumeration data type it will be displayed as a dropdown list of the values from the enumeration.
• Inheritance. Both Entity Framework and Linq to SQL support inheritance relationships in their data models. Dynamic Data will now properly display this data.
This sample also shows some other advanced features in Dynamic Data such as
• Multiple Data Models
• Each data model uses a different data model technology.
• Each registers its own custom DynamicData directory.
This sample project demonstrates new functionality that allows Dynamic Data field templates to be used by applications without any requirements (no data model is required (but can be used), no routing is required, no registration globally is required. Just take a data control and use the code DataControl.EnableDynamicData. This automatically gives the data control automatic validation and supports the attributes in System.ComponentModel.DataAnnotations.
• DataTable Sample. This sample shows how to use Dynamic Data with ADO.NET DataTable/DataSets including how to add metadata from System.ComponentModel.DataAnnotations to a DataTable.
• Set Insert Defaults Sample. This sample demonstrates a common customer’s request of something that is difficult to do in ASP.NET and Dynamic Data and that is to provide default values at runtime for fields in a data control that is in insert model (typically the developer uses FindControl to try and find the control and set its value). With this new functionality you can create an anonymous object and set the names of parameters to the value of choice ( new { Name = “DefaultName”; } ).
• ObjectDataSource Sample. This sample demonstrates Dynamic Data controls being used with the traditional ObjectDataSource. Just call EnableDynamicData with the type of the object being bound to ObjectDataSource and it all works.