Advanced Customization in ASP.Net Dynamic Data

Ambily.raj
Posted by in ASP.NET category on for Beginner level | Points: 150 | Views : 22982 red flag
Rating: 5 out of 5  
 1 vote(s)

ASP.Net Dynamic Data is used to develop data driven web applications using dynamic page and field templates. In this article, we will look into the various validation options and custom pages.

Advanced Customization in ASP.Net Dynamic Data

ASP.Net Dynamic Data is used to develop data driven web applications using dynamic page and field templates. We discussed about the ASP.Net Dynamic Data in the first article, discussed about the routing in the second article and discussed the customization in third article (Read my earlier article on Dynamic Data here). In this article we will look into the following questions.

  • How to implement validations?
  • Where to implement business logic?
  • How to remove or hide few columns from UI?
  • How to create a table specific form?

Attribute Validation

Let us look into the LINQ to SQL designer class file. You can find one class defined for each table we selected on the ORM designer. 



For our sample, we will use the Product table and corresponding class file. For doing any validation, define a new partial class with the same name as table name.

Add another class for defining the Metadata associated with the product table. We will be adding the field validations in the metadata class only.

Add selected fields to the Metadata class and specify the attribute validations. For our demo, I specified the required field validator for SupplierID field and range validator for UnitPrice field.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.ComponentModel.DataAnnotations;

 

namespace SampleDynamicWebApplication

{

    [MetadataType(typeof(ProductMetaData))]

    public partial class Product

    {

    }

 

    public class ProductMetaData

    {

        [Required]

        public object SupplierID;

       

        [Range(20,100)]

        public object UnitPrice;

    }

}


We can define the field type as object, it will take the field type from the partial class available as part of the LINQ to SQL designer class.
 
Run the application and edit the product table record. Observe the following validation messages corresponding to SupplierID and UnitPrice fields. 
 


Custom Field Validation

For adding custom validations before inserting a record to the table or on change of some values, we can use the Extension methods available in the designer classes.

Any table level validations like on inserting a product record, on deleting a customer record, etc., we can use the extensibility methods available in the datacontext class itself.



For field level validations like on changing the product name, on categoryid changed, etc., we can use the Extensibility methods available in respective table class.




For, our sample, I am using the OnProductNameChanging extensibility method.  I defined my validation as the productname should start with an upper case letter.

[MetadataType(typeof(ProductMetaData))]

    public partial class Product

    {

        partial void OnProductNameChanging(string value)

        {

            if (value != null && !Char.IsUpper(value[0]))

            {

                throw new ValidationException("Product Name should start with Upper case letter.");

            }

        }

    }


Try to edit or insert a product record.


Remove column from Dynamic Data Grid

We may need to hide few fields from the display. For example, we don’t want to display the ReorderLevel field in product table display.  Use the ScaffoldColumn attribute to hide the column from UI page.

[MetadataType(typeof(ProductMetaData))]

    public partial class Product

    {

        partial void OnProductNameChanging(string value)

        {

            if (value != null && !Char.IsUpper(value[0]))

            {

                throw new ValidationException("Product Name should start with Upper case letter.");

            }

        }

    }

 

    public class ProductMetaData

    {

        [Required]

        public object SupplierID;

       

        [Range(20,100)]

        public object UnitPrice;

 

        [ScaffoldColumn(false)]

        public object ReorderLevel;

    }


Look into the product table display, ReorderLevel field won’t be displayed.


Table Specific Custom Form

For adding table specific form to our ASP.Net Dynamic Data web application, create a folder with the table name under customPages folder. Add the table specific page to under the new folder. Her, in our sample, I added the List.aspx page for the Products table. 



 
Products table will use the new List.aspx page and the other tables will use the existing List.aspx page under PageTemplates.

Products table



Orders table


Conclusion

ASP.Net dynamic data has a very good support for validation and business logic implementation. We can validate the data in field level or in record level using various attributes and extensibility methods. Also, we can define table specific pages using Custom pages.

 

Page copy protected against web site content infringement by Copyscape

About the Author

Ambily.raj
Full Name: Ambily KK
Member Level: Silver
Member Status: Member,Microsoft_MVP,MVP
Member Since: 5/18/2010 1:05:25 AM
Country: India
Thanks Ambily K K http://ambilykk.com/
http://ambilykk.com/
I have over 9 years of experience working on Microsoft Technologies. I am carrying the passion on Microsoft technologies specifically on web technologies such as ASP .Net and Ajax. My interests also include Office Open XML, Azure, Visual Studio 2010. Technology adoption and learning is my key strength and technology sharing is my passion.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)