• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 441
  • Last Modified:

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?



0
mattegol
Asked:
mattegol
  • 2
  • 2
1 Solution
 
GuitarRichCommented:
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

0
 
mattegolAuthor Commented:
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.
0
 
GuitarRichCommented:
This site will probably explain how to use XML in .net better than I could here
http://www.c-sharpcorner.com/Articles/ArticleListing.aspx?SectionID=1&SubSectionID=79
http://www.functionx.com/xmlnet/index.htm
Basically my XML was something like in the code snippet below. I would store that in a varchar(max) field in the database with a form id, name etc...
Then when loading it in, I would parse through the XML using the XmlDocument object. Example below. Look up XPath in google too, that will help you navigate through the Xml document.
This is just a rough example as I haven't got my original source on me right now.
Hope it helps

<!----  XML Definition ---------!>
<form>
    <controls>
        <control id="control1" type="TextBox" size="100,23" position="0,0" label="Control 1:" />
        <control id="control2" type="TextBox" size="100,23" position="0,26" label="Control 2:" />
        <control id="control3" type="ComboBox" size="100,23" position="0,42" label="Control 3:" />
        <control id="control4" type="CheckBox" size="100,23" position="0,68" label="Control 4:" />
    </controls>
</form>
 
<!----- Code to load it in -----!>
private void LoadControls(string xml)
{
    XmlDocument dom = new XmlDocument();
    dom.LoadXml(xml);
 
    XmlNodeList list = dom.SelectNodes("/controls/control");
    foreach (XmlNode node in list)
    {
        switch (node["type"].Value)
        {
        case "TextBox":
            // create textbox here
            TextBox t = new TextBox();
            t.Name = node["name"].Value;
            // Set other properties etc...
            this.Controls.Add(t);
            break;
        case "ComboBox":
            // create combobox here
            break;
        // etc....
        }
    }
}

Open in new window

0
 
mattegolAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now