Try our conversational search powered by Generative AI!

Linus Ekström
Oct 30, 2008
  4264
(2 votes)

Inserting XForms as dynamic content

With the introduction of dynamic content in EPiServer CMS 5 comes a lot of new posibilities for the editors to make the page more dynamic. One sample that we shipped with the R2 release was the page property plugin. This makes it possible to insert a property from another page in a xhtml editor of a page. This can for instance be used to insert a simple value type such as a copyright text that is just a string property on another page.

As we use the Property web control to present the content of the property it's possible to insert more complex objects with the page property adapter as it just loads the PropertyControl associated with the PropertyData object. One great example is the XForms property. This makes it possible to insert a form on whatever page you want that has at least one xhtml string with dynamic content enabled. For this to work as expected we had to make some changes to the public templates as the form previously often had dependancies to the actual page/user control that was used to present the form. The XFormControl now has a static event, ControlSetup, that makes it possible to add a global behaviour to the XFormControl wherever it may be instantiated. Here is the actual code that attaches event to forms that is located in the global.asax.cs file in the R2 version of the public templates:

protected void Application_Start(Object sender, EventArgs e)
        {
            XFormControl.ControlSetup += new EventHandler(XForm_ControlSetup);
        }

public void XForm_ControlSetup(object sender, EventArgs e)
        {
            XFormControl control = (XFormControl)sender;

            control.BeforeLoadingForm += new LoadFormEventHandler(XForm_BeforeLoadingForm);
            control.ControlsCreated += new EventHandler(XForm_ControlsCreated);
            control.BeforeSubmitPostedData += new SaveFormDataEventHandler(XForm_BeforeSubmitPostedData);
            control.AfterSubmitPostedData += new SaveFormDataEventHandler(XForm_AfterSubmitPostedData);
        }

This makes it possible to still have your own custom logic when a form is loaded or posted even though you have no idea of where the form might appear. If you start to develop with the public templates in the R2 release you don't have to do anything to get this working but you should be aware that you will have logic to the form events in your global.asax.cs file. If you have an old project and upgrade it to R2 you will have to move your xform event handlers from your xform template page(s) to some global place, like the global.asax.cs file (or at least to attach your event handlers globally). You still have the option to attach local event handlers for specific form pages, just be aware that you might have global events that affects the form.

Oct 30, 2008

Comments

Oct 12, 2010 10:33 AM

Thank you for your article. I need to add a mailto everytime the editor adds a text field that validates to an email address. Do you have any clues how to do that? Thanks.
/ Victor Dollero (victor.dollero@fortunecookie.co.uk)

Please login to comment.
Latest blogs
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog