2010-01-07 47 views
1

我正在開發Quiz項目。在DetailsView中我想顯示問題和答案。ASP.net - 動態顯示控件

答案的數目變化爲問題question.Say示例

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 

,所以我不能修復的無線電buttons.Some問題的數量可以具有多個answers.so我需要顯示覆選框,而不是無線電鈕釦。

我的問題是如何生成單選按鈕或複選框動態?

回答

2

爲每個問題創建一個RadioButtonList或CheckBoxList,並使用它的Items集合添加答案。在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); 
    } 
} 
1

我覺得你的問題更具體是如何決定哪些測驗題有多個答案。

如果我是正確的,那麼你需要在DB中的表中有一個額外的列,如isMultipleAnswers BIT(或者你需要爲每個問題設置一個標記),然後處理DetailView事件,如DataBinding ,根據該字段檢查該字段的值,或者添加RadioButtonList或CheckBoxList。

希望這會有所幫助!

順便說一句,你爲什麼不使用直放站?