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

Dynamic Controls in ASP.NET Web Form

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!



0
gedmatt
Asked:
gedmatt
2 Solutions
 
hendridmCommented:
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"];
         quest.Controls.Add(t);
         break;

      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]));
         }
         quest.Controls.Add(c);
         break;
 
     case "D": // DROPDOWN MENU
         DropDownList dl = new DropDownList();
         dl.ID = "q" + dr["question_id"];
         dl.DataSrouce = someDataView;
         dl.DataBind();
         quest.Controls.Add(dl);
         break;
   }
}

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

-----------

This is just an example.  Hopefully you can make enough sense of it to change it to your needs.
0
 
raterusCommented:
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.
0
 
YZlatCommented:
''add a textbox

Dim tb As TextBox
        tb = New TextBox
        tb.Text = "My dynamic test"
        tb.Width = 200
        Me.Controls.Add(tb)

''add a checkbox:

Dim chk As TextBox
        chk= New TextBox
        chk.Checked=True        
        Me.Controls.Add(chk)
0
 
stengeljCommented:
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:radio.../>
</asp:panel>
<asp:panel ... expression='<%# [Question Type] = textbox %>'>
    <asp:textbox.../>
</asp:panel>
...etc.


0
 
gedmattAuthor Commented:
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.
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: 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.

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