Adding TextBocex, CheckBoxes on C # request

In the near future I will start the launch of the project, and the client would like to be able to add text fields or flags as an administrator on the portal, so, for example, initially I might have something like

Name [textBox] Phone [textBox]


Thus, the client wants to log in as an administrator and will be able to add

Name [textBox] Phone [textBox] Get brochure [checkBox] // added by client.

Forget about the portal and the admin part. What I would like to know is what would be the best way to develop it (user could add elements)

Any ideas would be highly appreciated

Hello

+4
source share
2 answers

You can create an additional aspx form in which the User (or Administrator) can define and create your own forms, you provide variable names and choose to add controls, save them in a specific scheme in the database, for example

UserForm: UserID FormID

Form: FormID FormName

FormElement: FormID VariableName IndexType Index

Of course, the administrator can also do this and be visible to everyone.

To view specific forms, you can add another aspx page containing the following code:

protected void Page_Load(object sender, EventArgs e) { //you saved the FormName or ID to Session when you accessed it string formName = Session["FormName"].ToString(); //this handles getting the elements for this Form from DB List<FormElement> elementList = FormElement.GetForForm(formName); this.renderForm(elementList); } private void renderForm(List<FormElement> eList) { foreach(FormElement fe in eList) { //Labels left, Controls right, of course this is just a design decision if(fe.Index%2==1) { Label lbl = new Label(); lbl.Text = fe.Variable; lbl.ID = fe.ControlType + fe.Variable; divLeft.Controls.Add(lbl); } else { dynamic ctrl = null; switch (fe.ControlType) { case "TextBox": ctrl = new TextBox(); break; case "CheckBox": ctrl = new CheckBox(); break; default: break; } ctrl.ID = fe.ControlType + fe.Variable; divRight.Controls.Add(ctrl); } } } 

Later, after the user clicked the submit button, you can get the values ​​entered into these controls by accessing divRight.FindControl (fe.ControlType + fe.Variable), as this should be unique for each form.

This approach assumes that you are using .NET 4.0 (because of the dynamic), but of course you can do it perfectly, without it, it will just be more code.

Please let me know if this is what you were looking for, or if it was helpful.

Thanks,

Dennis

+2
source

From what I did in the past, when I had to create a dynamic study based on customer needs. I had a database containing the following tables:

  • Survey - Keeps a list of customer surveys.
  • Controls - Lists the types of controls that are needed. For example, a text box, check box, etc.
  • Survey Controls - Connects all the controls needed for polling.
  • Custom Values ​​— Save the values ​​entered into the control based on the survey used.

Then I added some logic to dynamically create the controls based on the survey selected by reading the values ​​from my database.

+2
source

All Articles