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!

14 Comments

  • Hi,

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

    Thanks,
    Thani

  • Thanks,nice article.

  • This is excellent!

  • 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 ?

  • @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

  • 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.

  • 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.

  • 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.

  • 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?

  • @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 http://msdn.microsoft.com/en-us/library/cc488523.aspx

  • @Quan Pham

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

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


    Thanks.

  • @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.

  • @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.

Comments have been disabled for this content.