Best way to build a form dynamically

C

Codex Twin

Hello Control Builders::

Part of the form that I am designing has to be built dynamically. I
ultimately want to create a set of controls that are defined somehow in the
database as part of the form. I am mulling over the design on how to
implement this and I do not know what the best way to do that would be.

One idea I have considered is to write a control which uses a ControlBuilder
class. So I have the following attributes heading my custom control,
DynamicFormBuilder:

[ParseChildren(false)]
[ControlBuilder(typeof(MetaPropertyControlBuilder))]
public class DynamicFormBuilder : WebControl, INamingContainer
{...}

The class called MetaPropertyControlBuilder specified in the ControlBuilder
attribute above is a separate class that inherits from the
System.Web.UI.ControlBuilder class.

Following on, I need to pass in an XML representation of the child controls
that the control will contain. For this, I would create an XML file from the
database (by serialization I guess) that contains a definition of the
child-controls that are to be created by this Control.

The XML file would be something like this:

<ControlTextBox PropDefId="1" label="Name" name="txtName" />
<ControlTextBox PropDefId="2" label="Reference Number" name="txtRefNumber"
/>
<ControlTextBox PropDefId="3" label="Department" name="txtDepartment" />
<ControlDatePicker PropDefId="4" label="From" name="dateProjectFromDate" />

The second attribute specifies ParseChildren(false) which means that the
MetaPropertyFormCreator will read the child XML tags that we have specified,
and then proceed to parse the XML tags and return the type that we specify
to be associated with that tag. This is done by overriding the
GetChildControlType() method in the DynamicFormBuilder control.

Then from the XML, <ControlTextBox> tag would represent a textbox control
and a <ControlDatePicker> would return the type of a DatePicker control. The
additional attributes are properties that I would like to pass to the
created control. Hence the name is the name I wish to pass on as the id of
the control. And the label as the text to be used in the preceding TableCell
of the row that the control is contained in.

However, I am not still not sure how I would pass in the XML that is created
from the database to the control at run time.

Is this the way that anyone has used? Or am I barking completely up the
wrong tree and missed out lots of essential implementation? Bear in mind
that the above explanation is a gloss-over of creating a Control that uses
ControlBuilder.
I would be grateful if someone can suggest something better/easier?

Many thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top