views:

43

answers:

2

I am developing Quiz project. In DetailsView I want to display the Question and answers.

The number of answers vary for question to question.Say example

1 ) C# Support

   (i)  Generics
   (ii) LINQ
   (iii)EntLib  

2 ) Find the odd one
    (i)  DB2
    (ii) Oracle
    (iii)MS-Access 
    (iv) Sql Server
    (v)  Javascript

so i can not fix the number of radio buttons.Some questions may have multiple answers.so i need to display checkboxes instead of radio buttons.

My Question is how to generate Radio Buttons or Checkboxed Dynamically ?

+2  A: 

Create a RadioButtonList or CheckBoxList for each question and use its Items collection to add the answers.

Very simple example in C#:

class Question
{
    public string QuestionText;
    public List<string> Answers;
}

protected void AddQuestionsToContainer(Control container, List<Question> questions)
{
    foreach (Question q in questions)
    {
        var qt = new Label();
        qt.Text = q.QuestionText;
        container.Controls.Add(qt);

        var rbl = new RadioButtonList();

        foreach (string answer in q.Answers)
        {
            rbl.Items.Add(new ListItem(answer));
        }

        container.Controls.Add(rbl);
    }
}
Dan Story
+1  A: 

I think your question more specifically is how to decide which quiz question has multiple answers.

If I'm correct than you need to have an extra column like isMultipleAnswers BIT in the table in DB (or whatever the source is you need to have a flag for each question), and handle a event for the DetailView like DataBinding, check the value for this field, based on that either add RadioButtonList or CheckBoxList.

Hope this helps!

BTW, why aren't you using Repeater??

Manish