Dynamic Data in Regular Websites/Web Applications

Update: David Ebbo has a great video on channel9 about this. You can watch it here.

Today I'm excited to share that we've released DynamicData Preview 4 on codeplex. Check out the latest bits here.

This release is particularly interesting not only for people that have been using Dynamic Data for a while now, but anyone that has an existing application today who wants to use some of the niceties Dynamic Data offers without having to take all the junk associated. Take a look at the SimpleDynamicData project for examples.

Existing Sites

Here are 2 good reasons to use Dynamic Data:

  • Rich model validation
  • Rich templating support via FieldTemplates 

If you've ever written a data driven app in webforms using our data controls, you would have realized that we are lacking a lot when it comes to validation. You can enable all of this goodness with a magic extension method.

protected void Page_Init() {
    GridView1.EnableDynamicData(your type here);
}

Note: The method requires that you pass in the type that may have annotations in order for us to read Metadata. If you're not familiar with the way annotations work in Dynamic Data then watch the videos here.

This method call enables DynamicControl/DynamicField to work within any of the data controls which makes use of FieldTemplates, and enables the rich validation support.

Making it work

So we know about this magic method call and somehow calling it with a type makes it all just work. Let's walk through an example of how we would use this. Here is my model:

public class Student {
    [Required]
    [Range(0, 100)]
    public int Age { get; set; }
 
    [Range(0.0, 4.0)]
    public double GPA { get; set; }
 
    [Required]
    public string FirstName { get; set; }
 
    [Required]
    public string LastName { get; set; }
 
    [DisplayFormat(DataFormatString = "{0:d/M/yyyy}")]
    public DateTime BirthDate { get; set; }
}

This is the type we are going to use for metadata. Using the attributes from System.ComponentModel.DataAnnotations we can add useful annotations to our model that will be used for validation and display formatting. Adding these attributes allows Dynamic Data to enable the appropriate validator. i.e RangeValidator, RequiredFieldValidator etc.

Now we're going to enable this on our GridView using the same method call as above in conjunction with an ObjectDataSource to complete our application:

protected void Page_Init() {
    GridView1.EnableDynamicData(typeof(Student));            
}

Markup

<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1" AutoGenerateEditButton="true">        
    </asp:GridView>
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
        DataObjectTypeName="Student" DeleteMethod="DeleteStudent" 
        InsertMethod="InsertStudent" SelectMethod="GetStudents" 
        TypeName="StudentsRepository" UpdateMethod="UpdateStudent">
    </asp:ObjectDataSource>

Note: Dynamic Data takes care of the Metadata not the data. You still need databind the data control against some data source/data source control.

Here are the results when we are editing:

As you can see, the attributes we specified on our Student class directly affect the grid and validation is enabled.

There's more cool stuff to talk about but I'll mention those in upcoming posts. For now, download the preview and read up on Dynamic Data!

Published Wednesday, May 06, 2009 6:33 PM by davidfowl

Comments

# Twitted by davidfowl

Thursday, May 07, 2009 3:09 AM by Twitted by davidfowl

Pingback from  Twitted by davidfowl

# Dynamic Data in Regular Websites/Web Applications

Thursday, May 07, 2009 3:53 AM by What's New

Today I&#39;m excited to share that we&#39;ve released DynamicData Preview 4 on codeplex. Check out the

# Dynamic Data in Regular Websites/Web Applications

Thursday, May 07, 2009 4:59 AM by Web Development Community

You are voted (great) - Trackback from Web Development Community

# re: Dynamic Data in Regular Websites/Web Applications

Thursday, May 07, 2009 5:26 AM by MisterFantastic

Hi,

A very nice and cool post. Is that possible to customize the error messages that are shown.

Thanks,

Thani

# re: Dynamic Data in Regular Websites/Web Applications

Thursday, May 07, 2009 7:34 AM by ahsanm.m

Thanks,nice article.

# Adding ASP.NET Dynamic Data to an existing website with ASP.NET Dynamic Data Preview 4 &laquo; Bogdan Brinzarea&#8217;s blog

Pingback from  Adding ASP.NET Dynamic Data to an existing website with ASP.NET Dynamic Data Preview 4 &laquo;  Bogdan Brinzarea&#8217;s blog

# Dew Drop - May 7, 2009 | Alvin Ashcraft's Morning Dew

Thursday, May 07, 2009 8:53 AM by Dew Drop - May 7, 2009 | Alvin Ashcraft's Morning Dew

Pingback from  Dew Drop - May 7, 2009 | Alvin Ashcraft's Morning Dew

# re: Dynamic Data in Regular Websites/Web Applications

Thursday, May 07, 2009 12:00 PM by Saif Khan

This is excellent!

# Dynamic Data in Regular Websites/Web Applications | New Techs

Pingback from  Dynamic Data in Regular Websites/Web Applications | New Techs

# re: Dynamic Data in Regular Websites/Web Applications

Friday, May 29, 2009 12:45 PM by ali_karayel

it looks great but, i have not worked in my project. Actually, gridView does not have any method like this 'GridView1.EnableDynamicData()'. Any one has any idea ?

# re: Dynamic Data in Regular Websites/Web Applications

Friday, June 26, 2009 2:21 PM by davidfowl

@ali karayel

Hey ali GridView does not have a method, this is infact an extension method. To make it appear you need to add a reference to DynamicData and add:

using System.Web.DynamicData;

Hope this helps

# re: Dynamic Data in Regular Websites/Web Applications

Tuesday, August 11, 2009 2:50 AM by Meltac

Looks great, but unfortunately I didn't manage to get it working. No error, but no effect of my metadata as well :-(

Could You possibly post a (simple) sample project according to Your description in this post? Thx.

# re: Dynamic Data in Regular Websites/Web Applications

Sunday, August 16, 2009 10:43 PM by John

Dynamic Data seems very important to the future of ASP.NET keep on posting. Always good to hear from people that are a part of the team.

# re: Dynamic Data in Regular Websites/Web Applications

Sunday, November 15, 2009 1:06 AM by davidfowl

I'll modify the post and be more specific about the details. In order to get the Dynamic data behavior you have to use the dynamic controls i.e. <asp:DynamicControl /> <asp:DynamicField />

Leave a Comment

(required) 
(required) 
(optional)
(required)