As shown within our previous post called 'DevExpress XAF - Default Administration Items on Object Creation' we are able to set default values for new records when they are created to allow for easier data entry. Since then we have had the requirement to enter default text into a HTML editor field. To enter the default text was easy, just call the field and set the value to the string you wished to appear as default.
However we wanted to go further and to allow of even easier and quicker data entry, we wanted to enter a table with blank rows and column headings by default into a HTML editor field.
The first step was to create and gain the HTML for the table. To do this we found it easiest to run the project and manually create the table within the HTML editor field. Once the table was created how we required it to look, we viewed the HTML within the editor and took a copy of it.
Once we had a copy of the HTML we created a text file within the project and entered the HTML of the table within the file. We then change the build action of the text file to Embedded Resource and set Copy to Output Directory to Do not copy.
We now have the HTML of the table in the Embedded Resource text file within the project, we now needed to call the file and set the default HTML fields to the HTML within our file.
To do this, like the 'DevExpress XAF - Default Administration Items on Object Creation' blog mentioned, we will set it using the DevExpress AfterConstruction event within the business object. The code below shows how to get the HTML within your text file and set it as the default HTML within your HTML field.
/// <summary> /// Invoked when the current object is about to be initialized after its creation. /// </summary> public override void AfterConstruction() base.AfterConstruction(); var assembly = Assembly.GetExecutingAssembly(); var resourceName = "MySolution.Project.Module.Templates.DefaultHtml.txt"; using (Stream stream = assembly.GetManifestResourceStream(resourceName)) using (StreamReader reader = new StreamReader(stream)) this.HtmlField = reader.ReadToEnd();