I’ve been working with Dynamic Data websites on and off for the last couple of
weeks in order to prepare some screen casts I'm doing that demonstrate how to use the
Infragistics toolset with Dynamic Data.
As I started building my sample I quickly found that I needed
to pass arguments from a UIHint attribute in my model’s meta-data up to a field
template.
The context I am working in features two auditing columns
that are on each of my tables: CreatedOn and ModifiedOn.
The business rules are simple: when adding a record both
fields must have the current date and time. When editing a record the ModifiedOn
field must always have the current date and time – overwriting the existing data.
The field template I needed is easy to create, but it does
require that I pass in an argument telling the template whether or not to
overwrite the date or to simply add the data if nothing is there.
Here’s how I implemented my solution.
I updated the model with the following meta-data:
[MetadataType(typeof(Intervew_Metadata))]
public partial class Interview{}
public class Intervew_Metadata
{
[UIHint("DateTimeAutoFill")]
public object CreatedOn;
[UIHint("DateTimeAutoFill",null,"Overwrite","true")]
public object ModifiedOn;
}
My two date fields are decorated with the UIHint attribute signaling
to Dynamic Data that the control used to render these fields is my custom
DateTimeAutoFill templates. The ModifiedOn field is using a params list to pass
in a series of name/value arguments.
Here is my field template markup:
<%@ Control Language="C#" CodeFile="DateTimeAutoFill_Edit.ascx.cs" Inherits="DateTime_EditField" >
<%@ Register Assembly="Infragistics35.WebUI.WebDateChooser.v8.1, Version=8.1.20081.1000, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb"
Namespace="Infragistics.WebUI.WebSchedule" TagPrefix="igsch" >
<igsch:WebDateChooser value="<%# FieldValueEditString %>" id="WebDateChooser1" runat="server">
</igsch:WebDateChooser>
<asp:RequiredFieldValidator runat="server" id="RequiredFieldValidator1" controltovalidate="WebDateChooser1" display="Dynamic" enabled="false"></asp:RequiredFieldValidator>
<asp:DynamicValidator runat="server" id="DynamicValidator1" controltovalidate="WebDateChooser1" display="Dynamic"></asp:DynamicValidator>
The ASCX is pretty basic. All that is here is my UI control
and the needed validation controls.
Here’s the code-behind:
using System.ComponentModel.DataAnnotations;
...
protected override void OnPreRender(EventArgs e)
{
UIHintAttribute hint = null;
bool overwrite = false;
hint = (UIHintAttribute)this.Column.Attributes[typeof(UIHintAttribute)];
if (hint != null)
{
if (hint.Contro