Friday, July 16, 2010

Examine Columns in ASP.NET Dynamic Data MetaTable

This is how you get access to all columns in a MetaTable that serves as a data source to your controls like a DetailsView or a GridView. Dynamic Data generates the following code for each ASP.NET page by default:


   protected MetaTable table;

   protected void Page_Load(object sender, EventArgs e)
   {
       table = DetailsDataSource.GetTable();
       Title = table.DisplayName;
   }
The MetaTable class has a Columns property that returns a collection of MetaColumns for the table. This allows us to enumerate through the collection and check for any custom attributes on table columns:
    
   foreach (MetaColumn column in table.Columns)
   {
      if (column.Attributes.OfType<MyCustomAttribute>().Count() > 0)
          ......
      }
   }

This is based on a tip found at this blog.

No comments:

Post a Comment