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.
Working with Dynamic Language Code in the DataList Control
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
- Switch to Source view.
- 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.
- Click Ctrl+F5 to run the page, and verify that the page looks the same.
- 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
- Switch to the code for the page, and add the following import statement:
IronPython
from System.Drawing import Color
- 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
- Return to the Web page markup, replace the text CategoryName: with the text The <%# CategoryName.upper() %> category includes: and remove the
CategoryNameLabelLabel 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.
- Remove the text Description: above the
DescriptionLabelcontrol, and add a BackColor attribute to theDescriptionLabelcontrol. 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
ColorPickermethod, changing the background color of descriptions for category names longer than ten characters. - 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.