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 - Unhandled Exception

Thursday, May 07, 2009 1:39 PM by DotNetShoutout

Thank you for submitting this cool story - Trackback from DotNetShoutout

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

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

# Dynamic Data in Regular Websites/Web Applications | Free Tutorial 4 All

Pingback from  Dynamic Data in Regular Websites/Web Applications | Free Tutorial 4 All

# 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 />

# Enhance WebForms Apps in Minutes with EnableDynamicData

Friday, May 21, 2010 8:52 AM by Web Dev Blog

[View:http://vimeo.com/11909470] Enhancing ASP.NET Apps in Minutes Until recently using DynamicData required

# re: Dynamic Data in Regular Websites/Web Applications

Wednesday, April 20, 2011 3:04 PM by Prnda1976

This is all great in simple example with text boxes but what would you do if you had a drop down list with values and you need it validated(value must be selected)? You cannot use the asp:DynamicControl anymore since you need drop down list? What would you do then?

# re: Dynamic Data in Regular Websites/Web Applications

Thursday, April 21, 2011 5:56 AM by davidfowl

@Prnda1976 You can use dynamic control in that case and write a custom field template to do that kind of validation yourself. Read more here msdn.microsoft.com/.../cc488523.aspx

# re: Dynamic Data in Regular Websites/Web Applications

Tuesday, May 17, 2011 1:16 AM by Quan Pham

Hi David, I've tried everything but still won't be able to enable the method EnableDynamicData(). I create a very simple website with just one page with a gridview. I add System.Web.DynamicData and also import that in the code. Is it because I'm using VB that I can't do this? If not, how would you suggest me to do it.

Thanks a lot.

# re: Dynamic Data in Regular Websites/Web Applications

Tuesday, May 17, 2011 5:49 AM by davidfowl

@Quan Pham

Does you GridView have AutoGenerateColumns=true? Or do you have explicit fields defined?

# re: Dynamic Data in Regular Websites/Web Applications

Tuesday, May 17, 2011 11:01 AM by Quan Pham

@David

I don't explicitly set it to true but I believe that's the default right? Here is the mark up:

<asp:GridView ID="GridView1" runat="server" AllowPaging="true" CssClass="myGrid" GridLines="None" PageSize="15">

Thanks.

# re: Dynamic Data in Regular Websites/Web Applications

Tuesday, May 17, 2011 2:26 PM by davidfowl

@Quan Pham

There's big difference between having AutoGenerateColumns VS not having it. If you have explicit fields defined, you need to replace BoundField with DynamicField. If you have auto generate columns to true it does it behind the scenes.

# re: Dynamic Data in Regular Websites/Web Applications

Tuesday, May 17, 2011 2:26 PM by davidfowl

@Quan Pham

There's big difference between having AutoGenerateColumns VS not having it. If you have explicit fields defined, you need to replace BoundField with DynamicField. If you have auto generate columns to true it does it behind the scenes.

Leave a Comment

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