2014-02-27 43 views
0

我在數據列表中有單選按鈕。我需要在後面的代碼中訪問它們。但是我所有的努力都沒有奏效。這是我在做什麼:無法訪問代碼後面的代碼中的單選按鈕

背後
<asp:DataList ID="dlPaper" runat="server" RepeatColumns="1" RepeatDirection="Vertical" 
      Width="755px" onitemcommand="item_command"> 
      <ItemTemplate> 
       <table> 
        <tr> 
         <td> 
          Question : 
         </td> 
         <td colspan ="4"> 
          <asp:Label ID="lblQuestion" runat="server" Text='<%#Eval("Question") %>'></asp:Label>? 
           <asp:Label ID="lblQID" runat="server" Text='<%#Eval("QID") %>' Visible="False"></asp:Label> 
         </td> 
        </tr> 
        <tr> 
        <td></td> 
         <td> 
          A 
         </td> 
         <td> 
          <asp:Label ID="lblOption1" runat="server" Text='<%#Eval("Option1") %>'></asp:Label> 
         </td> 
         <td> 
          B 
         </td> 
         <td> 
          <asp:Label ID="lblOption2" runat="server" Text='<%#Eval("Option2") %>'></asp:Label> 
         </td> 
        </tr> 
        <tr> <td></td> 
         <td> 
          C 
         </td> 
         <td> 
          <asp:Label ID="lblOption3" runat="server" Text='<%#Eval("Option3") %>'></asp:Label> 
         </td> 
         <td> 
          D 
         </td> 
         <td> 
          <asp:Label ID="lblOption4" runat="server" Text='<%#Eval("Option4") %>'></asp:Label> 
         </td> 
        </tr> 

       </table> 
       <table> 
       <tr> 
         <td> 
          Answer : 
         </td> 
         <td>  
           <asp:RadioButton ID="rdOption1" runat="server" Text="A" GroupName="Ans"/> 
         </td> 
         <td> 
         <asp:RadioButton ID="rdOption2" runat="server" Text="B" GroupName="Ans" /> 
         </td> 
         <td> 
         <asp:RadioButton ID="rdOption3" runat="server" Text="C" GroupName="Ans" /> 
         </td> 
         <td> 
         <asp:RadioButton ID="rdOption4" runat="server" Text="D" GroupName="Ans" /> 
         </td> 
         <td> 
        <asp:Button ID="tbnAnswer" runat="server" Text="Submit Answer" CommandName='<%#Eval("QID") %>' OnCommand='item_command'/> 
        </td> 
        </tr> 
       </table> 
      </ItemTemplate> 
     </asp:DataList> 

代碼如下:

protected void item_command(object source, CommandEventArgs e) 
{ 
    try 
    { 

     _ds = new DataSet(); 

     int p = WebHelper.Cast(e.CommandName, 0); 


     ViewState["QID"] = p; 
     AnswersBAL abl = new AnswersBAL(SessionContext.SystemUser); 

     abl.LoadByQID(_ds, p); 
     if (_ds != null && _ds.Tables[abl.SqlEntityX].Rows.Count > 0) 
     { 
      int AID = System.Convert.ToInt32(_ds.Tables[0].Rows[0]["AID"]); 

      abl.LoadByPrimaryKey(_ds, AID); 
      screenscrap(_ds.Tables[abl.SqlEntityX].Rows[0]); 

      abl.Save(_ds); 
     } 
     else 
     { 
      abl.LoadByPrimaryKey(_ds, int.MinValue); 
      DataRow dr = _ds.Tables[abl.SqlEntityX].NewRow(); 
      _ds.Tables[abl.SqlEntityX].Rows.Add(dr); 
      dr["QID"] = System.Convert.ToInt32(ViewState["QID"]); 
      dr["StdID"] = 1; 
      //sessioncontext.systemuser 

      //dr["Option1"]= // 

      abl.Save(_ds); 
     } 

     } 


    catch (Exception err) 
    { } 
} 

但我不能夠訪問單選按鈕在後面的代碼。

回答

0

嘗試這樣

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e) 
    { 

    if (e.CommandName == "YourCommandName") 
      { 
       r1 = (RadioButton)e.Item.FindControl("rdButton"); 
      } 
    }