How to dynamically add form controls from a dropdownlist

Posted on 2009-04-28
Last Modified: 2012-05-06
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?

Question by:mattegol
    LVL 12

    Expert Comment

    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...

    Open in new window


    Author Comment

    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.
    LVL 12

    Accepted Solution

    This site will probably explain how to use XML in .net better than I could here
    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 ---------!>
            <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:" />
    <!----- Code to load it in -----!>
    private void LoadControls(string xml)
        XmlDocument dom = new XmlDocument();
        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...
            case "ComboBox":
                // create combobox here
            // etc....

    Open in new window


    Author Closing Comment


    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Suggested Solutions

    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now