无法在后面的代码中访问datalist中的单选按钮

时间:2014-02-27 12:27:51

标签: c# asp.net

我在datalist中有单选按钮。我需要在后面的代码中访问它们。但我所有的努力都没有奏效。这就是我在做的事情:

<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)
    { }
}

但我无法在后面的代码中访问单选按钮。

1 个答案:

答案 0 :(得分:0)

试试这个

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {

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