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.
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)