Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2008-11-04
1
Medium Priority
?
1,631 Views
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?

Thanks
0
Comment
Question by:danburyit
1 Comment
 
LVL 1

Accepted Solution

by:
danburyit earned 0 total points
ID: 22877164
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: http://msdn.microsoft.com/en-us/library/cc488527.aspx

        partial void OnContextCreated()
        {
            this.SavingChanges += new
                System.EventHandler(OnSavingChanges);
        }
 
        /// <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

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

577 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