Dynamic Controls in ASP.NET Web Form

Posted on 2005-04-26
Last Modified: 2010-04-07
Hi there,

I am working on an ASP .NET application to manage the creation of questionnaires. Questionnaires are composed of a number of Questions in a specified order. Questions can be of type bool, select, int, string, decimal etc. (note that these "types" are simply names used to decribe different types of questions rather than actual C# data types).

Depending on the question type, a different control needs to be displayed accordingly i.e. a checkbox for bool type, a text field for string type or a dropdown list or the select type. My question is how best to go about achieving this in a ASP .NET Web Form? The controls will also need data to be bound (i.e. the dropdownlist would display a list of possible answers). I would ideally like to keep the code in the code behind if possible rather than mess around with the HTML bit.

Any help much appreciated!

Question by:gedmatt
    LVL 8

    Accepted Solution

    You could use a switch().  Short example:

    foreach(DataRow dr in myRecordSet) {
       switch(dr["question_type"].ToString()) {

          case "T":  //TEXTBOX
             TextBox t = new TextBox();
             t.ID = "q" + dr["question_id"];

          case "C":  // CHECKBOX COLLECTION
             CheckBoxList c = new CheckBoxList();
             c.ID = "q" + dr["question_id"];
             temp = dr["options"].ToString().Split(";"); // semicolon used as a delimiter of options
             for(int i=0;i<temp.Length;i++) {
                c.Items.Add(new ListItem(temp[i], temp[i]));
         case "D": // DROPDOWN MENU
             DropDownList dl = new DropDownList();
             dl.ID = "q" + dr["question_id"];
             dl.DataSrouce = someDataView;

    <form runat="server" id="quest" />


    This is just an example.  Hopefully you can make enough sense of it to change it to your needs.
    LVL 33

    Expert Comment

    I'd try to design this page around a repeater, inside the repeater's itemtemplate you have a custom control "question", this control has a property "questiontype", and a datasource for the databound controls.  This control knows how to render itself properly for each control type (in CreateChildControls).  This is a way to avoid the dynamic control mess, because dynamic controls are awful and should always be avoided.
    LVL 35

    Expert Comment

    ''add a textbox

    Dim tb As TextBox
            tb = New TextBox
            tb.Text = "My dynamic test"
            tb.Width = 200

    ''add a checkbox:

    Dim chk As TextBox
            chk= New TextBox
    LVL 9

    Assisted Solution

    I did the same thing with a survey recently...

    In my database, I created a table for Questions.  Each question was assigned a type (textbox, checkbox, dropdownlist, etc.).  Then I created a Choices table that linked to the Questions table.  This way I could assign unique choices for each question as well as define the question type.

    Then, in my .aspx page I created panel that would display the linked choices based on the question type...

    <asp:panel ... expression='<%# [Question Type] = dropdownlist %>'>
    <asp:panel ... expression='<%# [Question Type] = textbox %>'>


    Author Comment

    Thanks to everyone... I have finally managed to get it all working. I went with hendridm's method in the end, although encountered a few problems with getting the user input data from the form elements and ended up using the Request.Form collection (and a bit of code I found on the web).

    I have given stengelj some of the points too as I did a quick test with this method and it seemed to work really well and I would consider using that in future. But as it's working no point starting over!

    Thanks again.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Join & Write a Comment

    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    729 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

    21 Experts available now in Live!

    Get 1:1 Help Now