Dynamic Controls in ASP.NET Web Form

Posted on 2005-04-26
Medium Priority
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

Accepted Solution

hendridm earned 400 total points
ID: 13868511
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

ID: 13868672
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

ID: 13869502
''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

Assisted Solution

stengelj earned 100 total points
ID: 13883116
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

ID: 13884898
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Screencast - Getting to Know the Pipeline
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

862 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