Databinding with Dynamic Languages for ASP.NET

06/21/2009
Prerequisites

In order to complete this walkthrough, you will need the following:

  • 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.
  • The Northwind.mdb file that contains the Access version of the sample Northwind database. This file is included in the ASP.NET Quickstarts. If you installed the ASP.NET Quickstarts with Visual Studio, the file is located in the QuickStart\aspnet\samples\data\App_Data folder. Alternatively, you can use another database and adjust the steps in the walkthrough to match the database and tables that you are using.

 

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 Using Dynamic Languages with ASP.NET), you can use that Web site and go to the next part of the walkthrough, where you add an Access database to the project. Otherwise, create a new Web site and page by following these steps.

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

  1. Copy the files from the ASP.NET Dynamic Language Support project into an empty directory.
  2. In Visual Studio (or Visual Web Developer), in the File menu, click Open Web Site. The Open Web Site dialog box is displayed.
  3. 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.

  4. Click Open. Visual Studio opens the folder as a website and displays the files in the Solution Explorer.

Next you will add the Northwind.mdb Access database or your own database to the Web application project.

To add an Access database to the project

  1. If the Web site does not already contain an App_Data folder, in Solution Explorer, right-click the name of your Web site, click Add ASP.NET Folder, and then click App_Data.
  2. In Visual Studio, in Solution Explorer, right-click the App_Data folder, and then click Add Existing Item.
  3. In the Add Existing Item dialog box, browse to the Northwind.mdb file, and then click Add.
Posted in: Website-asp.net| Tags: .NET asp.net Dynamic Data Prerequisites Website database asp dynamic visual file walkthrough order

Working with Dynamic Language Code in the DataList Control

06/21/2009

In this part of the walkthrough you will use dynamic language code to perform data binding instead of using the Eval method that is generated by default for declarative data binding.

To use dynamic language code with the DataList control

  1. Switch to Source view.
  2. Remove the Eval methods from the DataList1 control markup. When you are finished, the DataList control markup will look like the following:
    <asp:DataList ID="DataList1" runat="server"
    DataSourceID="AccessDataSource1">
    <ItemTemplate>
    CategoryName:
    <asp:Label ID="CategoryNameLabel" runat="server"
    Text='<%# CategoryName %>'>
    </asp:Label><br />
    Description:
    <asp:Label ID="DescriptionLabel" runat="server"
    Text='<%# Description %>'>
    </asp:Label><br />
    <br />
    </ItemTemplate>
    </asp:DataList>

    The data bindings are now simply dynamic language code. Because the code is dynamic, the field names can be resolved using late binding.

  3. Click Ctrl+F5 to run the page, and verify that the page looks the same.
  4. Close the browser.

Because the bindings are dynamic language code, you can use them to change the way the field values are displayed. In this part of the walkthrough, you will change the case of the CategoryName field, and change the background color of the Description field depending on the length of the CategoryName field.

To use dynamic language code to change the appearance of fields

  1. Switch to the code for the page, and add the following import statement:

    IronPython

    		
    from System.Drawing import Color
  2. Add the following function to return a color based on the size of a string:

    IronPython

    		
    def ColorPicker(input):
    input = str(input)
    if len(input) > 10:
    return Color.Yellow
    else:
    return Color.White
  3. Return to the Web page markup, replace the text CategoryName: with the text The <%# CategoryName.upper() %> category includes: and remove the CategoryNameLabel Label control from the item template.

    When you are finished, the item template will look like the following.

    IronPython

    		
    <ItemTemplate>
    The <%# CategoryName.upper() %> category includes:
    Description:
    <asp:Label ID="DescriptionLabel" runat="server"
    Text='<%# Description %>'>
    </asp:Label><br />
    <br />
    </ItemTemplate>

    The dynamic language compiler will resolve the field name and apply the ToUpper() method to the field. Be sure to include the parentheses. If you omit them, the compiler generates an object representing the method itself, which will not display anything.

    Note   You can use either the Python upper method or the .NET Framework ToUpper method.

  4. Remove the text Description: above the DescriptionLabel control, and add a BackColor attribute to the DescriptionLabel control. When you are finished, the item template will look like the following.

    IronPython

    		
    <ItemTemplate>
    The <%# CategoryName.upper() %> category includes:
    <asp:Label ID="DescriptionLabel" runat="server"
    Text='<%# Description %>'
    BackColor='<%# ColorPicker(CategoryName) %>' >
    </asp:Label><br />
    <br />
    </ItemTemplate>

    The dynamic language compiler will evaluate the expression and call the ColorPicker method, changing the background color of descriptions for category names longer than ten characters.

  5. Press Ctrl+F5 to run the page and verify that the category name appears in upper case, and that the description has a yellow background color whenever the category name is longer than ten characters.
Posted in: .NET Framework Website-asp.net| Tags: Dynamic Data DataList Control Code IronPython

ASP.NET Dynamic Language Support

06/11/2009
Introduction
Dynamic Languages are a class of high-level programming languages that do not rely on static typing. Many decisions that are made at compile time by a statically typed language are instead made at run time by a dynamic language. For example, many dynamic languages use dynamic typing, where an object’s type is determined at run time instead of at compile time. These languages make a trade-off between compile-time type-checking in favor of increased flexibility at run time.
There are many good static languages, such as C#, and many good dynamic languages, such as IronPython. The choice of what type of language to use comes down to personal preference and to the nature of the project you’re working on.
Giving ASP.NET users the choice of languages was part of the design since our first version of ASP.NET, and this Dynamic Language Support is just another step in that direction. Unlike other Web platforms that support only one language, the ASP.NET team wants to enable users to choose the language that fits them best.
This Release
This release is compatible with IronPython 2.6 Beta 1. Currently it does not include Language Services Support and project templates. To create a new IronPython ASP.NET WebForms project, simply copy the “examples\hello-webforms” to a new directory and rename the directory to your liking. A redistributed copy of IronPython 2.6 Beta 1 can be found in the “bin” directory; all files except Microsoft.Web.Scripting.dll, the IronPython ASP.NET integration, are from the IronPython 2.6 Beta 1 release.
Included in this release are two WebForms examples that are written in IronPython: “hello-webforms” and “album-handler”, which can be found in the “examples” directory. “hello-webforms” is a simple web application that shows PostBack handling, and “album-handler” is a larger web application that creates a photo album from a directory of images and generates thumbnails for them on the fly.
Current Limitations
There are some tooling limitations with this release:
  • Limited support for designers
  • No IntelliSense support.
  • No support for ASP.NET MVC. This is planned in the future by extending the IronRuby ASP.NET MVC support: http://github.com/jschementi/ironrubymvc.
Why?
If you are an ASP.NET developer interested in trying out the simplicity and flexibility of a dynamic language, then this is for you - or if you are already investigating IronPython, then this opens up the door to developing ASP.Net Web applications using that language. For those interested in building ASP.NET applications with IronRuby, that's coming. Posted in: Website-asp.net| Tags: asp.net Dynamic Language Support Introduction Support languages run language class asp dynamic

ASP.NET 4.0 AJAX Preview 4

06/11/2009

The preview contains the following new features:

  • ADO.NET Data Services support
  • WCF and ASMX Web service integration
  • ASP.NET AJAX Client Templates
  • Declarative instantiation of client-side controls and behaviors
  • Observable pattern for plain JavaScript objects
  • Live Bindings
  • Markup Extensions
  • DataView control and DataContext component
  • Command Bubbling
  • Change tracking and identity management
  • Support for managing complex links and associations between entities from multiple entity sets or tables
  • Extension methods allowing change tracking and read-write client-server scenarios using other JSON services, including RESTful or JSONP based services
ASP.NET AJAX templates let you specify the UI for data controls by using HTML markup. You can create dynamic pages such as master-detail views that render data from a Web service or an ADO.NET data service, with little or no JavaScript code. The UI is rendered dynamically in the browser, and it can respond automatically to changes in the data without requiring any request or postback to the server. This preview provides full support for read-write scenarios, in which data changes in the client are uploaded to the server in a single batch update.
Download: ASP.NET AJAX 4.0 Preview 4
Note: To use the new features in a Web page, you must first enable the updated version of Microsoft Ajax on the page.
See the Asp.Net Ajax Preview 4 Readme for details.
New with this release:
  • Full Client Reference documentation
  • A set of Code Samples using ASP.NET AJAX templates
The Code Samples will be updated from time to time, with additional samples.
The last update to the samples was on March 23rd 2009. Posted in: Website-asp.net| Tags: WCF asp.net ajax Preview ado.net

Hot Posts

Latest posts

Tags

Others

Sponsors