Custom Dynamic Data field templates - update date, update by - non-editable

Posted on 2008-11-04
Last Modified: 2013-11-08
Hi All,

In a Dynamic Data website, I'd like to create a custom field template(s) (or use some other means) of displaying read only values for update date and update by and submit the current user and date with inserts and edits. Anyone know of good examples?

Question by:danburyit
    1 Comment
    LVL 1

    Accepted Solution

    OK, I ran across this solution pretty quick. I also forgot to mention that I'm using a Entity Data Model (as opposed to SQL).  The following code goes in a partial class of the Entity type created for the Dynamic Data site.

    I figured this out from this article:

            partial void OnContextCreated()
                this.SavingChanges += new
            /// <summary>
            /// Updates modified by and date fields for updates
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            public void OnSavingChanges(object sender, System.EventArgs e)
                var stateManager = ((TransitScheduleEntities)sender).ObjectStateManager;
                var changedEntities = stateManager.GetObjectStateEntries(EntityState.Modified);
                foreach (ObjectStateEntry stateEntryEntity in changedEntities)
                    int modBy = stateEntryEntity.CurrentValues.GetOrdinal("ModifiedBy");
                    int modDate = stateEntryEntity.CurrentValues.GetOrdinal("DateModified");
                    if (modBy > 0)
                        stateEntryEntity.CurrentValues.SetValue(modBy, HttpContext.Current.User.Identity.Name);
                    if (modDate > 0)
                        stateEntryEntity.CurrentValues.SetValue(modDate, DateTime.Now);

    Open in new window


    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now