2011-07-15 85 views
0

我正在使用中繼器控件向用戶顯示在線問題紙張。我向用戶展示了50個問題。我給每個問題4個複選框來選擇答案。現在我的疑問是如何獲得用戶檢查的所有50個選項,並將這些答案與我的XML中的正確答案標記進行比較。我正在使用XML文件,而不是數據庫。如何從中繼器控制中獲取值

任何人都可以請幫助我如何實現此功能?

+0

你有什麼嘗試?你應該張貼一些代碼來顯示你到目前爲止的內容。 –

回答

0

你必須遍歷Repeater控制,如...

if (Repeater1.Items.Count > 0) 
{ 
    for (int count = 0; count < Repeater1.Items.Count; count++) 
    { 
     CheckBox chk = (CheckBox)Repeater1.Items[count].FindControl("CheckBox1"); 
     if (chk.Checked) 
     { 

     } 
    } 
} 
0

要訪問Repeater商品,則應該使用:

repeaterId.Items 

要訪問所有選中的一箇中繼器的控件(這是絕對RadioButton控件,你應該有每個問題一個選項),你可以使用:

foreach (ListViewDataItem item in repeaterId.Items) 
{ 
    // Finding RadioButton controls by Id 
    RadioButton firstOption = ((RadioButton)item.FindControl("firstOption")); 
    RadioButton secondOption = ((RadioButton)item.FindControl("secondOption")); 
    RadioButton thirdOption = ((RadioButton)item.FindControl("thirdOption")); 
    RadioButton fourthOption = ((RadioButton)item.FindControl("fourthOption")); 
    // Here you have four RadioButtones and you should only see which one of them is clicked. Then compare its value to correct value in your XML file. 
} 
+0

ListViewDataItem只存在於.NET 3.5以上。 Repeater.Items中Item的正確類型爲RepeaterItem。 – SwissCoder

+0

另外,最好不要將控件明確地強制轉換爲RadioButton,而是使用下面的代碼:item.FindControl(「FirstOption」)作爲RadioButton; – SwissCoder