Dynamic ASP.net Web Forms

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

An administrator needs to define various types of fields, such as check boxes, radio buttons, text fields and text fields to be filled in by the user. The administrator can also determine if these custom fields are needed.

I am now at the planning stage, and I am interested in how I will store these custom field definitions in the database and how I can put them out and make them functions.

EDIT

The form data that end users provide to these dynamically created form fields must also be stored in the database.

How can I solve this problem?

+4
source share
6 answers

Without doing this or really anything like that, I would think that your database structure may include the type of control, its name, the type of its value, if it is confirmed, how to check it, if necessary. Then, when you read the records / dataset you need, you probably compare the type of control in the recordset with the translated value and then add it to the web form.

For instance:

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 theoretically places a label next to the text box control. I don't know if the Div will work, but you can use this type of stuff for CheckBox, TextBox, Label, etc.

After the controls and labels are on the page, you will need some form of action to force the server to save the values ​​in the database. I would recommend storing the name of the table and column with a list of objects. Then map the form field to the database column to insert. (keep in mind that this explanation is similar to 200,000 feet).

+4
source

Here is what I quickly hacked to prove the concept.

DynamicForms.zip

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

I built it fast, so it's pretty sloppy.

+6
source

You may need the following basic structure: Form, Field, FieldType. Each of them needs business objects and data tables, so you can store configurations for each field.

Most likely, you will need a field to set several properties (for example, required) so that you can track this independently for each field.

Then you need some kind of utility that can read the data level, and then for each field and field type know what type of control you need to do and how to add controls to the current page.

Using the asp: PlaceHolder tag on the main page allows you to dynamically add controls to this place owner using the form creation utility.

Finally, you will most likely want to create a style sheet with several classes for different parts of the created form, so that the client can customize fonts, spacing, etc. any table of controls that you create.

EDIT:

If the data entered on the form needs to be saved, you need to decide if you want to have a custom data level. You may have an empty database table in which you use ALTER TABLE scripts to allow the client to add columns to the database as needed, and then you can set which control is bound to which column. This mapping between the management column and the database will be critical to the proper storage of data.

+2
source

Each control can be created 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 get the control after the postback, you can use the FindControl method.

 TextBox name = (TextBox) Panel1.FindControl("name of control"); 
0
source

As others have said, there are certain ways to do this by dynamically adding controls to an ASP.NET page at run time.

Alternatively, although I have not used Microsoft Infopath , from what I heard about it, it does quite a lot of what you do (i.e. allow superusers to create forms / profiles and put them on the Internet and collect data back)

If your client has the appropriate version of Microsoft Office, they may already have InfoPath.

0
source

I have worked on this before. For this, I used two tables, say

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

and

KeyField_Details: for storing values ​​of custom fields in terms (value and description).

Use the Page_Load event to create these 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 { // ... } 

You can also refer to this question, "How to check dynamically created controls?" Is this question related to this procedure?

In addition, the check can be client-side, as described in the previous question? or on the server side, storing the created fields in a list, then you can check this list at the time of sending.

0
source

All Articles