日期:2014-05-18  浏览次数:20746 次

如何循环获取radiobutton的值


C# code

<asp:DataList ID="DataList1" runat="server" Height="241px" Width="775px">
                    <ItemTemplate>
                        <table>
                            <tr>
                                <td>
                                    <%#Container.ItemIndex+1 %>
                                    、<asp:Label ID="timu" runat="server" Text='<%#Eval("question") %>'></asp:Label>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:RadioButton ID="A" runat="server" GroupName="radion" />A、<asp:Label ID="Label1"
                                        runat="server" Text='<%#Eval("A") %>'></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;
                                    <asp:RadioButton ID="B" runat="server" GroupName="radion" />B、<asp:Label ID="Label2"
                                        runat="server" Text='<%#Eval("B") %>'></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;
                                    <asp:RadioButton ID="C" runat="server" GroupName="radion" />C、<asp:Label ID="Label3"
                                        runat="server" Text='<%#Eval("C") %>'></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;
                                    <asp:RadioButton ID="D" runat="server" GroupName="radion" />D、<asp:Label ID="Label4"
                                        runat="server" Text='<%#Eval("D") %>'></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;
                                </td>
                            </tr>
                        </table>
                    </ItemTemplate>
                </asp:DataList>




------解决方案--------------------
首先在 DataList里面加入一个页眉按钮 代码如下 ,修改按钮的CommandName为submit这个名字
<FooterTemplate>
<asp:Button ID="Button1" runat="server" CommandName="submit" 
OnClick="submit_Click" Text="提交" />
</FooterTemplate>

然后在页面在 DataList 控件双击事件 ItemCommand
代码如:
C# code

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "submit")
            {
                for (int i = 0; i <= e.Item.Controls.Count; i++)//根据你一个有多少个选项循环多少次
                {
                    RadioButton A = (RadioButton)DataList1.Items[i].FindControl("A");//第i选题第1个选项
                    if (A.Checked) 
                    {
                        Label Label1 = (Label)DataList1.Items[e.Item.ItemIndex].FindControl("Label1");
                        string value = Label1.Text;
                    }
                    RadioButton B = (RadioButton)DataList1.Items[i].FindControl("B");//第i题第2个选项
                    if (A.Checked) 
                    {
                        Label Label2 = (Label)DataList1.Items[e.Item.ItemIndex].FindControl("Label2");
                        string value = Label2.Text;
                    }
                    RadioButton C = (RadioButton)DataList1.Items[i].FindControl("C");//第i题第3个选项
                    if (A.Checked) 
                    {