Dynamic Data Details

At this point, you might be looking at the preceding example and wondering what the big deal is . After all, if you point the GridView toward a data source, the GridView automatically reflects the data source columns and creates a bunch of columnar representations . However, the columns for the GridView shown so far are bound columns that use the BoundField server-side control. Once the GridView is rendered—that's all the end user sees, and there is no easy way to customize the behavior. ASP.NET Dynamic Data features change that: The Dynamic Data features are built around the DynamicControl and the DynamicField controls .

The DynamicControl is used in the ListView and the FormView data controls . Using the DynamicControl causes ASP.NET to render a UI based on schema from the database . You use the DynamicField in the GridView and DetailsView (instead of the BoundField) to render the UI. The DynamicField relies on the DynamicControl internally, so produces the same sort of data-driven UI in the context of the GridView and the DetailsView.

When you use the DynamicControl (either explicitly or implicitly), ASP.NET renders data entry forms generated directly from the tables of your database, and you can substitute other controls to render the UI (rather than relying on the default provided by ASP.NET) . This lets you use a wide variety of controls easily in the GridView, ListView, FormView, and DetailsView.

For example, imagine you want to allow users to enter a Social Security number using the DataGrid. If you do not use Dynamic Data, the GridView uses the standard TextBox control to render the editing UI, and there's no easy way to inject any other control. Now imagine you want to use a different control during editing: the MaskedTextBoxExtender from the AJAX toolkit . By applying the MaskedTextBoxExtender to the TextBox, you can ensure that the user enters the number in the correct format (three digits, a hyphen, two digits, another hyphen, and then four more digits) . Up until now, it has not been feasible to extend the GridView in this way. But now you can with the DynamicControl or DynamicField. The Dynamic Data scaffolding produced by Visual Studio includes well-delineated places where you can substitute different, nondefault controls . In addition, Dynamic Data supports automatic validation. For example, by using the Dynamic Data features of ASP.NET, you can avoid having to recode all those validators over and over again because the database schema can inform how the data validation works for a particular column

The next example illustrates how to extend the application to show more tables and to use the automatic validation supplied by the scaffolding.

Extending the application

1. Open the DotNetReferences . dbml file. Visual Studio should display the Object

Relational Designer. Open Server Explorer. Find the DotNetLinks table and drag it into the Object Relational Designer. Rebuild the application and you will see links for the

DotNetLinks table (as well as the DotNetReferences table). Click the DotNetLinks hyperlink. You should see the DotNetLinks table represented in the browser as follows:

J DotNetLinks - Windows Internet Explorer

^□jxj

w |http:,','localhost: 1470/0utNetLinb/ List.aspy. [gjj [+t| [xj Bing

%% Favorites | ■¿J Suggested Sites ▼ £J Web Slice Gallery

DotNetLinks | ® ' El * 3 ^

0 ■* Page SafetyT Tools * J>

Dynamic Data Site

.. < Back to home page ....................................

DotNetLinks

DispIayName

URL

Details

Edit

Delete

Details

Microsoft ASP.NET page

http://www.asp.net

The official Microsoft,,,

Edit

Delete

Details

Microsoft Silverlight...

http://www. microsoft, c,,,

The official Microsot...

Edit

Delete

Details

Microsoft Windows Clie...

http://windowsdient.net/

The official Microsoft,,,

Edit

Delete

Details

Microsoft SQL Server a...

http ://msdn, microsoft,,,,

The official Microsoft,,,

Edit

Delete

Details

Channel 9

http : //channel?, msdn. com/

Channel 9— great inte...

Edit

Delete

Details

.NET Framework informa...

http://www. microsoft....

The official Microsoft,,,

Edit

Delete

Details

Microsoft Windows Comm...

http://msdn, microsoft,,,,

The official Microsoft,,,

Edit

Delete

Details

Windows 7

http://www. microsoft, c,,,

The official Microsoft,,,

^ Insert new item

^ Insert new item

I Internet | Protected Mode: On

14-h

2. Close the browser and open the file List .aspx; it is in Solution Explorer under the Project node: Open the Dynamic Data node, and then open the Page Templates node . After you open List . aspx, switch to Design mode if not already displayed as such. You should see the GridView in Design mode:

3. This page is just like any other Web form at this point . If you would like to format the GridView, you can by clicking the SmartTag for the grid (the angle bracket (>) in the box in the upper right corner of the control) . For example, you can auto format the

GridView to give it a more colorful appearance . You can also edit the control to have alternating row styles, as shown in Chapter 10 . The example here skins the GridView using the Classic format . Here's the DotNetLinks GridView following the reformat:

(5 DotNetLinks - Windows Int

irnet Explorer

^^, -w http: //localhost: 1470/DotNetLinks/List, aspx

zjE ** * lb M

Favorites ^ Suggested Sites ' Web Slice Gallery -

DotNetLinks

1 1

a • s • s &

» Page » Safety •» Tools •

- «- •

Dynamic Data Site

.. < Back to home page ....................................................................................................

DotNetLinks

Edit Delete Details

Microsoft ASP.NET page

http:// www.asp.net

The official Microsoft...

Edit Delete Details

MicrosoFt Silverlight...

http: //www. microsoft .c...

The official Microsot...

Edit Delete Details

Microsoft Windows Clie.,.

http! //windowsdient, net /

The official Microsoft.,.

Edit Delete Details

Microsoft SQL Server a.,.

http: //msdn, microsoft

The official Microsoft.,.

Edit Delete Details

Channel 9

http: //channel?, msdn .com/

Channel 9~ great inte,.,

Edit Delete Details

,NET Framework informa.,,

http! //www. microsoft

The official Microsoft.,.

Edit Delete Details

MicrosoFt Windows Comm...

http: //msdn. microsoft....

The oFficial Microsoft...

Edit Delete Details

Windows 7

http: //www. microsoft .c...

The oFf icial Microsoft...

+ Insert new item

Ö) Internet | Protected Mode: On

- MO

0% -

4. Now update the Linq to SQL data model to support regular expression validation. The file created by Visual Studio is named DotNetReferences .Designer, cs, and you can edit it by expanding the DotNetReferences . dbml file in Solution Explorer. Find the URL property in the DotNetLink class and add the RegularExpression attribute to it . The regular expression shown here is borrowed from the RegularExpressionValidator's ValidationExpression dialog box:

[g1oba1::System.Data.Linq.Mapping.Co1umnAttribute(Storage="_URL",

DbType="NVarChar(100) NOT NULL", CanBeNu11=fa1se)] [System.ComponentModel.DataAnnotations.RegularExpression

("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?")]

public string URL {

return this._URL;

this.OnURLChanging(value); this.SendPropertyChangingO; this._URL = value; this.SendPropertyChanged("URL"); this.OnURLChanged();

5. Run the site. Go to the DotNetLinks page and try to edit one of the links . You are presented with the DotNetLinks edit page . Type an invalid URL in the edit box. Click the Update link and watch the built-in regular expression validator fire, just as shown in Chapter 6, "Control Potpourri," and as illustrated in the following graphic . This happens because the URL property in the data model throws an exception when a value not matching the regular expression defined in the attribute is set .

I DotNetLinks - Windows Internet Explorer

^jnjxj

iJiifL^ » |jîi http://localhost:1470/DotrJetLinksMt.aspx?ID=5 - /s |t) Bing

Ph

Favorites | ^ Suggested Sites Web Slice Gallery »

DotNetLinks | | ® ' 0 ' 3 # ' Page '

Safety -

Tools * *

Dynamic Data Site

Edit entry from table DotNetLinks

List of validation errors

• The field URL must match the regular expression 'http(s)?://([\w-]+V)+[\w-]+(/[\w- ./?%&?=]*)?'.

DisplayName Microsoft Silverlight Page

URL [aaaaaaa | *

D et ai Is The official Microsot Silve

ppdate j Cancel

| Internet | Protected Mode: On

_Pa^

1 +„ 100% - J,

6 . To see where the built-in RegularExpressionValidator is set up, open the file

Text_Edit . ascx, which is in the DynamicData\FieldTemplates folder in Server Explorer. You'll see the following code:

<%@ Control Language="C#" CodeBehind="Text_Edit.ascx.cs" Inherits="DynamicDataLinqToSQLSite.Text_EditFie1d" %>

<asp:TextBox ID="TextBox1" runat="server"

Text='<%# FieldValueEditString %>' CssC1ass="DDTextBox"></asp:TextBox>

<asp:RequiredFie1dVa1idator runat="server"

ID="RequiredFieldValidator1" CssClass="DDControl DDValidator"

ControlToValidate="TextBox1"

Display="Dynamic" Enabled="false" />

<asp:RegularExpressionValidator runat="server"

ID="RegularExpressionValidator1" CssClass="DDControl DDValidator" ControlToValidate="TextBox1" Display="Dynamic" Enabled="false" />

<asp:DynamicValidator runat="server" ID="DynamicValidator1" CssClass="DDControl DDValidator" ControlToValidate="TextBox1" Display="Dynamic" />

Voila! The editing facilities of the page are controlled by a user control!

As you can see, there is nothing hidden or magical about the ASP.NET Dynamic Data feature. In fact, if you peruse the files generated by Visual Studio, you can see a host of common ASP.NET idioms and features that you have explored in earlier chapters of this book . The power of Dynamic Data is that you can use it to tailor a site that is driven by the data model rather than spending a lot of time creating a UI to match the data model. This is especially powerful if you expect your data model to change in the future.

0 -1

Average user rating: 1 stars out of 1 votes

Post a comment

  • Receive news updates via email from this site