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 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 Studio. For an introduction, see Walkthrough: Creating a Basic Page in Visual Web Developer.
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
- 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.
- 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.
- Right-click the page and click View Code, and then add import statements for the new module using the following code:
IronPython
from SampleModule import SampleClass
- Add an event handler for the Click event of the button, with the following code:
IronPython
def Button1_Click(sender, args): sc = SampleClass() sc.TestString = TextBox1.Text Label1.Text = sc.TestString
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.
- 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 />
- Press CTRL+F5 to run the page.
- 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.
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.