Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When creating a new record, how do I fill some fields with values from the filter?

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!)

like image 414
Wim ten Brink Avatar asked Nov 13 '09 10:11

Wim ten Brink


2 Answers

Answered Here: 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

like image 137
Aaron Hoffman Avatar answered Sep 27 '22 23:09

Aaron Hoffman


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.

like image 26
Samuel Meacham Avatar answered Sep 28 '22 00:09

Samuel Meacham