Share via


Automatically prepopulate fields for Insert in .NET 4

We introduced a lot of little data enhancements in .NET 4 that you may not have heard about. One of these is a mechanism for setting the default value of fields in a data control when you want to do an Insert. In our controls today you would probably do something painful:

- Handle the ItemCreated event and inside the event you can call FindControl and find the control for each column manually and set the value on the control using whatever property it supports to set its control.

- Handle the Inserting event on the data source control (if you are using one) and check to see if fields have values and if not set the default. This is problematic because this happens after the user has pressed Insert so they do not see the default value until AFTER the insert operation.

One of the features we enabled in .NET 4 was the ability to enable Dynamic Data functionality with a single line of code in an existing web page. By enabling Dynamic Data you can easily use some of its features to make this super simple.

In my example I’ve got a table called Contact which has an Id, FirstName, LastName and Age fields. I’ve created an Entity Framework model around my database giving me rich classes for the tables in my database. Here is what my page markup looks like:

    1: <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" 
    2:     AutoGenerateRows="True" DataKeyNames="Id" DataSourceID="EntityDataSource1" 
    3:     DefaultMode="Insert">
    4:     <Fields>
    5:         <asp:CommandField ShowInsertButton="True" />
    6:     </Fields>
    7: </asp:DetailsView>
    8: <asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=DatabaseEntities" DefaultContainerName="DatabaseEntities" EnableFlattening="False" EnableInsert="True" EntitySetName="Contacts">
    9: </asp:EntityDataSource>

And here is what my code behind markup looks like:

    1: DetailsView1.EnableDynamicData(typeof(DatabaseModel.Contact));
    2: var contact = new DatabaseModel.Contact();
    3: contact.FirstName = "Scott";
    4: contact.LastName = "Guthrie";
    5: DetailsView1.SetMetaTable(DetailsView1.GetMetaTable(), contact);

My code is doing a couple of items here:

1: Enabling Dynamic Data functionality for the data control passing it the type of data the control is hosting. This is the object from my Entity Framework model.

2 – 4: Creating a dummy record of Contact and filling in the fields with whatever data I want to use for my defaults.

5: Calling the SetMetaTable method on the data control passing the MetaTable which I get from the GetMetaTable method on the control and then passing my object that contains the default values.

Now if I run my page I will get the DetailsView to show up with my defaults automatically set. Note in my example I’m using auto generated columns. If you choose to manually specify your columns you will need to specify them using either DynamicField (DetailsView/GridView) or DynamicControl (FormView/ListView) for this functionality to work because these types inject Dynamic Data fields templates which allow this insert functionality to work.

Also you can also specify the defaults using a dictionary if you don’t want to create a full object. Here is an example of what that would look like:

    1: DetailsView1.EnableDynamicData(typeof(DatabaseModel.Contact));
    2: var contact = new Dictionary<string, object>() {
    3:     {"FirstName", "Scott"},
    4:     {"LastName", "Guthrie"}
    5: };
    6: DetailsView1.SetMetaTable(DetailsView1.GetMetaTable(), contact);

I’ve created a fully running sample of this you can download as well. The sample requires .NET 4, VS 2010 and SQL Express 2008 to be installed to run. Here is the link the sample: https://www.cshonline.com/aspnet/insertdefaults.zip