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

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?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

danburyitAuthor Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.