Asp.net dynamic data website: passing default when inserting / updating records

I work in an ASP.NET Dynamic Data web application and have the problem of passing the default when inserting / updating records. In my application, the whole table has the following common column:

  • CreatedBy (default: registered user)
  • CreatedDate (default: DateTime.Now)
  • modifiedBy (default: registered user)
  • ModifiedDate (default: DateTime.Now)

I want these columns to be hidden on the page Insertand I want Editthis default value to be automatically inserted into the corresponding column.

Please suggest me.

Thanks Paul

+3
source share
4 answers

The solution I implemented finally:

protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
   {
      e.Values.Add("CreatedBy", HttpContext.Current.User.Identity.Name);
      e.Values.Add("CreatedDate", DateTime.Now);
    }

 protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
      e.OldValues.Add("ModifiedBy", null);
      e.OldValues.Add("ModifiedDate", null);
      e.NewValues.Add("ModifiedBy", HttpContext.Current.User.Identity.Name);
      e.NewValues.Add("ModifiedDate", DateTime.Now);
   }
0
source

I see that you are doing a simple audit, check out my blog here. Basic audit for dynamic data with Entity Framework 4.x hope that helps.

It is never good to do such things on a page; it is always better to do this in your model / data layer. You can use the New way to create columns in dynamic data ... to hide columns on all pages.

+2
source

Ref:
- 1

, , - "". , , , , , .

:

/?

ORM Entity Framework, :

Default Entity Framework?
. -

0

.

In the DynamicData \ PageTemplates \ Insert.aspx.cs file, I made changes to show my default values ​​for new records in any table that has common fields. The user can still add something else to insert.

public partial class Insert : System.Web.UI.Page
{
    protected MetaTable table;

    protected void Page_Init(object sender, EventArgs e)
    {
        table = DynamicDataRouteHandler.GetRequestMetaTable(Context);
        var values = table.GetColumnValuesFromRoute(Context);

        // set default values for meta data of new records across all tables
        // unknown values will be skipped
        values.Add("creationDate", DateTime.Now);
        values.Add("modificationDate", DateTime.Now);
        values.Add("modificationUser", HttpContext.Current.User.Identity.Name.Substring(
            HttpContext.Current.User.Identity.Name.IndexOf("\\") + 1));

        FormView1.SetMetaTable(table, values);
        DetailsDataSource.EntityTypeFilter = table.EntityType.Name;
    }
    [...]
}

To edit records with existing values, I made changes to some DynamicData \ FieldTemplates files.

public partial class Text_EditField : System.Web.DynamicData.FieldTemplateUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // ...

        // show current user as value for the modification user upon editing records
        if (Column.Name == "modificationUser")
        {
            FieldValue = Page.User.Identity.Name.Substring(Page.User.Identity.Name.IndexOf("\\") + 1);
        }
    }
    [...]
}

It will show the updated value on the page for editing, but after updating the changes will not be saved! An additional change to the Edit Page template is required:

    protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
        // make sure a meta data update is always triggered by setting a different old value
        // required for the edit components
        if (e.OldValues.Contains("modificationUser"))
        {
            e.OldValues["modificationUser"] = string.Empty;
            e.OldValues["modificationDate"] = DateTime.MinValue;
        }
    }
0
source

All Articles