4

I need to create a survey page with the following structure read from database.

Survey QuestionA
a) Answer1 [Radio button]
b) Answer2 [Radio button]
c) Answer3 [Radio button]
d) Answer4 [Radio button]

repeats..

The page has many questions that needs to be dynamically added. I need to store the result of the form on in an array of Question object on submit. One way I know to do this is create dynamic UI in a table and get the values by FindControl. Is there a better (elegant) way to do this?

3
  • I came across the same problem couple of weeks ago and had to recursivley find the corresponding controls after the page was submitted; I'd like to know if there is a more elegant way, too :) Commented Dec 6, 2011 at 12:52
  • and I believe you need to recreate UI also on postback. Commented Dec 6, 2011 at 12:53
  • I have recently answered a somewhat similar question here. Commented Dec 29, 2011 at 22:03

5 Answers 5

2

In ASP.Net MVC it handles the dirty work for you with default model binders. Of course you can also create your own. Though this does not give you the automatic solution you were hoping for in ASP.Net Web Forms, my preference in this situation would be to follow a similar common pattern that ASP.Net MVC is using for it's naming convention thus simplifying it. You could then start writing code that could be reused over time. Here is a link to an article explaining the naming convention on Haack's blog http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx.

Long term recommendation is to come to ASP.Net MVC, life is just better here :)

Sign up to request clarification or add additional context in comments.

Comments

2

I suggest create a userconrol that implement a question(label) and answers(radio buttons) and each controls(labels,radios) is binded to a property of your usercontrol, Then you can read questions from database and for each data create this usercontrol object and set correspond data to that property of usercontrol, And to read data from control this state doing vice versa.

Albeit you must recreate usercontrols in each post back and set default data to those.

Also you can create multiple usercontrols with different UI that inherit a interface such as IQuestion, and a factory class that create each of usercontrols depend of environment varibles.

3 Comments

+1 agreed. Looking through the DOM for data is a performance nightmare.
@Craig I was implying "don't even think about switching to client-side code for data retrieval, no matter how you implement this". Ok, maybe that wasn't so clear, my bad.
@alex sorry alex, I didn't see that answer. Thanks for pointing that out. I've removed my comment to save any confusion. Cheers!
0

You can use jQuery to get the selected radio buttons by the checked property and append them with the question number. e.g. for question 1 you have

So you can get the values iterating throught the radio buttons like so: $("input[type='radio']").checked

Comments

0

You could always add the controls in programmatically.

Say you have a aspnet Panel control you can perform...

RadioButton rb = new RadioButton();
rb.ID = "rbRadioButton";
rb.Name = "rbRadioButton";
rb.cssClass = "radioClass";
Panel1.Controls.Add(rb);

Excuse the harshness of the example, not got visual studio to hand at the moment to check it but I hope you get the idea. You could in essence build up the whole question this way based purely on the database. The downside is getting the values as you have to override the Render method (if I remember right, it has been a while, I do have an example if you want me to find it).

I admit it is a little overkill but it is a possible solution.

Comments

0

use radiobuttonlist (which you can see in the the toolbox ,it is a asp.net standard control) control for this purpose. It is up to your needs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.