Link to home
Start Free TrialLog in
Avatar of mattegol
mattegolFlag for Sweden

asked on

How to dynamically add form controls from a dropdownlist

I want to have a dropdownlist that contains different controls like textbox, checkbox, radiobutton and so on. When selecting one of the controls from the dropdownlist and submitting a button it should appear, selecting another it should appear under the first...

I'm going to use a database to select, insert, update the forms that the user chooses.

When choosing a control and clicking the button it inserts the controls to a table in the database, later you should be able to get the whole form on another page with the selected controls from the table.

What are your suggestions to do this?



Avatar of GuitarRich
GuitarRich
Flag of United Kingdom of Great Britain and Northern Ireland image

Sounds like your trying to create a dynamic form builder. I've done a similar thing - in my DB I saved an xml definition of the form with the type of control, a unique id size, placement etc...
Then when loading up the form I parsed the xml and dynamically added the controls to the form at runtime.
Adding a control dynamically at runtime is pretty simple, just create a new instance of the required control and add it to the forms controls collection. Example below:

// Work out which type of control to add from the XML - in this case its a textbox.
TextBox t = new TextBox();
t.Name = "control1TextBox";
// Set size and position here etc...
this.Controls.Add(t);

Open in new window

Avatar of mattegol

ASKER

Yes that's basicly what I'm looking for, would you like to show me a full example please? I haven't worked with XML that much.
ASKER CERTIFIED SOLUTION
Avatar of GuitarRich
GuitarRich
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks