Pre-Populating Custom Page Type Field with other fields that are required

Matthew Hudock asked on February 8, 2022 20:14

Hi All,

So I have a custom page type where I am asking for contact information above others First Name and Last Name are both required, however there is a use case for a full name field as well. Is it possible to get Kentico to take the information submitted in the First and Last name fields to pre-populate the Full Name field?

Thank You!

Correct Answer

vasu yerramsetti answered on February 9, 2022 13:50

Create the following code snippet on CustomPageEventHandler.cs file at \App_Code\Global\

using CMS.Base;
using CMS.DocumentEngine;
using CMS.Membership;

[CustomPageEventHandler]
public partial class CMSModuleLoader 
{
    /// <summary>
    /// Summary description for CustomPageEventHandler
    /// </summary>
    public class CustomPageEventHandler : CMSLoaderAttribute
    {
        /// <summary>
        ///     Called automatically when the application starts
        /// </summary>
        public override void Init()
        {
            DocumentEvents.Insert.After += Insert_After;
            DocumentEvents.Update.After += Update_After;
        }
        /// <summary>
        /// Insert after Event Handler for the creation of documents in content tree.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Insert_After(object sender, DocumentEventArgs e)
        {
            var treeNode = e.Node;
            if (e.Node.ClassName.Equals("custom.ContactTest"))
            {
                treeNode.SetValue("FullName", e.Node.GetValue("FirstName") + " " + e.Node.GetValue("LastName"));
            }
            treeNode.Update();
        }

        /// <summary>
        /// Update after Event Handler for the creation of documents in content tree.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Update_After(object sender, DocumentEventArgs e)
        {
            var treeNode = e.Node;
            if (e.Node.ClassName.Equals("custom.ContactTest"))
            {
                treeNode.SetValue("FullName", e.Node.GetValue("FirstName") + " " + e.Node.GetValue("LastName"));
            }
            treeNode.Update();
        }

    }
}
0 votesVote for this answer Unmark Correct answer

Recent Answers


Dmitry Bastron answered on February 9, 2022 05:32

Hi Matthew,

If all three fields are displayed at the same time - no, unlikely. What I would suggest is to hide Full Name field from the form, and then inside "before create/update page event" combine first name and last name and save it as full name.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.