views:

754

answers:

6

I am creating an ASP.NET application in which the administrator of the program will be able to define custom fields for forms that the users will fill out and submit.

The admin needs to define various types of fields like checkboxes, radio buttons, textboxes, and textareas for the user to fill out. The admin also can define whether these custom fields should be required.

I am in the planning stages now and am wondering about how I will store these custom field definitions in the database and how I will render them out and make them function.

EDIT

The form data that is submitted by end users with these dynamically created form fields also has to be persisted in the database.

How can I tackle this problem?

A: 

Every control can be instantiated and added to the page. Example:

 Label label = new Label();
 CheckBox check = new CheckBox();

After that you should add the control to the Controls property of any container:

Panel1.Controls.Add(label);

To retrieve the control after a postback you can use the method FindControl.

TextBox name = (TextBox) Panel1.FindControl("name of control");
fbinder
+2  A: 

The following basic structure will likely be needed: Form, Field, FieldType. You'll need business objects and data tables for each so you can store the configurations for each field.

You'll likely need the field to have several properties that can be set (like being required) so that you can track this independently for each field.

You'll then need some sort of utility that can read the data layer, and then for each field and field type know what type of control to make, and how to add the controls to the current page.

Using an asp:PlaceHolder tag on the main page will allow you to dynamically add controls to this place holder using your form building utility.

Finally, you'll likely want to build a style sheet with several classes for different portions of the form that are generated so that the client can customize the fonts, spacing, etc. of whatever table of controls you generate.

EDIT:

If the data entered on the form needs to be persisted, then you need to decide if you want to have a customizable data layer. You can have an empty database table that you use ALTER TABLE scripts on to allow the client to add columns to the database as needed, and then you can set which control binds to which column. This mapping between control to database column will be crucial to storing the data correctly.

Jay S
+3  A: 

Without having done this or really anything like it, I would think your DB structure might include the type of control, its name, its value type, if its validated, how its validated, if its required. Then as you are reading through the records/dataset of controls desired, you would probably compare the control type in the recordset to the translated value and then add it to the webform.

For example:

if(drow["ControlType"].ToString().ToUpper() == "TEXTBOX")
{
    Label lbl = new Label();
    lbl.Text = drow["ControlLabel"].ToString();
    TextBox txt = new TextBox();

    //TO GET THE VALUE ON POSTBACK LATER
    txt.ID = drow["ControlID"].ToString();

    //PROBABLY NOT NECESSARY
    Div myDiv = new Div();
    myDiv.Controls.Add(lbl);
    myDiv.Controls.Add(txt);

    //ADD THE DIV ABOVE TO THE FORM/PANEL/DIV/ETC.
    MyForm.Controls.Add(myDiv);
}

This in theory would put the label next to the textbox control. I have no idea if the Div would work, but you can run through this type of thing for CheckBox, TextBox, Label, etc.

Once the controls and labels are on the page, you'll want some form of action to cause the server to save the values to a DB. I'd recommend storing the table and column name with the list of objects. Then map the form field over to a DB column for the insert. (keep in mind this explanation is like at 200,000 feet)

RSolberg
A: 

As others have said, there are definately ways of doing this by dynamically adding controls to an ASP.NET page at runtime.

Alternatively, although I havent used Microsoft Infopath, from what I've heard about it, it does quite a lot of what you are trying to do (i.e. allow super-users to design forms/questionnaires and put on the web and gather the data back in afterwards)

If your client has the appropriate Microsoft Office edition, they might have InfoPath already.

codeulike
+2  A: 

Here is something I have whipped up real quick for proof of concept.

DynamicForms.zip

I built in VS 2008 SP1. There is a sql server db in the appdata folder, so sqlexpress will help if you want to run this.

I built this quickly, so it's pretty sloppy.

Ronnie Overby
+1 for effort :)
Paul Suart
A: 

I worked on this before. I used two tables for this, let's say

KeyField_Master: for field name, type ,and is it mandatory or not?

and

KeyField_Details: to store custom fields value in term of (value and description).

Use Page_Load event to create that fields

if (keyField_type == "T") // Textbox
{
   txtBox.Attributes.Add("Type", "T"); // type of field for validation
   txtBox.Attributes.Add("IsKeyField", "Y"); // to mark it as a custom field
   if (isMandatory == "Y")
     txtBox.Attributes.Add("IsMandatory", "Y"); // is it mandatory

   // you could set layout of these controls using HTML Tables or any other way you prefer.

  htmlCell.Controls.Add(txtBox);
  htmlRow.Cells.Add(htmlCell);
  tbHTML.Rows.Insert(2, htmlRow);
}
else if // other controls type
{
  // ...
}

Also, you could refer to that question "How to validate dynamically created controls?" This question is related to that procedure?

Also, validation could be client-side as described in above question? or server-side by storing created fields in a list then you could check that list in time of submit.

Ahmed