2012-04-07 67 views
1

下面的數據列表代表問題和答案的集合,當用戶點擊使用javascript提交按鈕時如何檢查用戶是否選擇了正確的答案單選按鈕?如何獲取數據列表中選定的單選按鈕文本?

答案存儲在數據庫中。

Datalist中:的CommandName = 「驗證」

添加OnItemCommand事件的DataList1:

<asp:DataList ID="DataList1" runat="server" DataKeyField="Qno" 
     DataSourceID="SqlDataSource1"> 
     <ItemTemplate> 
      Qno: 
      <asp:Label ID="QnoLabel" runat="server" Text='<%# Eval("Qno") %>' /> 
      <br /> 
      Question: 
      <asp:Label ID="QuestionLabel" runat="server" Text='<%# Eval("Question") %>' /> 
      <br /> 
      <asp:RadioButton ID="RadioButton1" runat="server" Text='<%# Eval("Ans1") %>' /> 
      <br /> 
      <asp:RadioButton ID="RadioButton2" runat="server" Text='<%# Eval("Ans2") %>' /> 
      <br /> 
      <asp:RadioButton ID="RadioButton3" runat="server" Text='<%# Eval("Ans3") %>' /> 
      <br /> 
      <asp:RadioButton ID="RadioButton4" runat="server" Text='<%# Eval("Ans4") %>' /> 
      <br /> 
      <asp:Button ID="Button2" runat="server" Text="Submit" /> 
      <br /> 
     </ItemTemplate> 
    </asp:DataList> 
+0

你有什麼試過的? – HBP 2012-04-07 05:52:12

回答

0

在DataList控件提供了將Button2的CommnadName參數* OnItemCommand = 「DataList1_OnItemCommand」 *

在後面的代碼,填寫在* DataList1_OnItemCommand *事件操作:

protected void DataList1_OnItemCommand(object sender, DataListCommandEventArgs e) 
{ 
if (String.Equals(e.CommandName, "Validate")) 
{ 
    DataListItem dataItem = (DataListItem)e.Item; 
    RadioButton rbtn1 = (RadioButton)dataItem.FindControl("RadioButton1"); 
    RadioButton rbtn2 = (RadioButton)dataItem.FindControl("RadioButton2"); 
    RadioButton rbtn3 = (RadioButton)dataItem.FindControl("RadioButton3"); 
    RadioButton rbtn4 = (RadioButton)dataItem.FindControl("RadioButton4"); 

    // Code to check which radio button was checked. 
    if(rbtn1 != null && rbtn1.Checked) 
    { 

    } 
    else if(rbtn2 != null && rbtn2.Checked) 
    { 

    } //Perform these for the remaining two check boxes 
} 
} 

根據選中的複選框執行必要的操作。

+0

我得到錯誤沒有重載'DataList1_OnItemCommand'匹配委託'System.Web.UI.WebControls.DataListCommandEventHandler' – Abhishek 2012-04-07 07:22:28

+0

如何解決錯誤? – Abhishek 2012-04-07 10:14:11

+0

早些時候我沒有檢查IDE中的語法就提供了答案。我現在通過修復一些不正確的語法來編輯我以前的答案。 – 2012-04-07 15:09:18

相關問題