views:

164

answers:

3

I'm working on an ASP.NET page, which basically is a quick hack around a database. It's used for an internal project and the site is set up in a way to provide several people read/write access to the data. Once a week, we collect a bunch of data from it, add it to an XML file and send it as part of an application update to our customers. (Those customers don't have direct access.)

Since it's just an internal project, there's almost no budget available for it's development. So we chose to keep things simple. We stored the data in an SQL Server database, created an Entity Framework class around this for data access and we put a Dynamic Data Site web application around this. Basically, something that can be set up real fast and without writing much code. It works quite well, too. Especially the filtering of records through boolean fields and table references is real cool.

However, during data entry a few users make minor mistakes. They've set up the filters to just filter a subset of a table and then click "New" to add a record to this subset. Unfortunately, the new record isn't defaulting to the values in those filters so users have to set the right values again. Too bad they occasionally miss this, thus some records end up with the wrong values.

So, when a user creates a new record, how do I ensure that this new record will copy the filter values as default? (And still allow the user to pick other values!)

A: 

I'd use an Object Factory hooked into the New button onclick. I'd initialize the Factory with either the Filter values or an interface to the View so that the Factory can return the properly initialized objects.

Brett Veenstra
+1  A: 

Is there an event that fires when they create a new record? If you can have implement an event handler, and the current set of filters is something you can programmatically access, then in your newRecord() event handler, you ought to be able to go through each of your current filters, determine which field the filter is for, and what the value of the filter is, setting the new record's field to the filter value.

Here's some pseudo-code if it helps:

NewRecordHandler(object sender, NewRecordEventArgs e)
{
    Record newRecord = (Record)e.NewRecord;
    foreach(Filter filter in m_dataSource.Filters)
    {
     newRecord[filter.FieldName] = filter.Value;
    }
}

You'll need to be able to dynamically access the records properties, using either an index or a string, like Record["ColumnName"] = value;. Otherwise you may not be able to do it in a loop, as illustrated. Hope this helps.

Samuel Meacham
Good question, since I'm just using the default functionality that VS2008 offers with the "Dynamic Data Site" code. Unfortunately, it doesn't do this by default and haven't had much time to search for a solution. (Right now, this is just a nice-to-have that will be added once I know a solution. We're not actively searching, since other tasks have higher priority.)
Workshop Alex
+1  A: 

Answered Here: http://stackoverflow.com/questions/1795249/dynamically-set-defaults-for-scaffolded-tables-according-to-dynamic-filters

Check Dynamic Data Futures "Populate Insert templates with values from filters": http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=14475

Also, this blog post by Stephen Naughton: http://csharpbits.notaclue.net/2009/01/getting-default-values-from-list-page.html

Aaron Hoffman
The "Dynamic Data Futures" link was extremely useful!
Workshop Alex