首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

怎么循环获取radiobutton的值

2012-04-25 
如何循环获取radiobutton的值C# codeasp:DataList IDDataList1 runatserver Height241px Width

如何循环获取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)                     {                        Label Label3 = (Label)DataList1.Items[e.Item.ItemIndex].FindControl("Label3");                        string value = Label3.Text;                    }                    RadioButton D = (RadioButton)DataList1.Items[i].FindControl("D");//第i题第4个选项                    if (A.Checked)                     {                        Label Label4 = (Label)DataList1.Items[e.Item.ItemIndex].FindControl("Label4");                        string value = Label4.Text;                    }                }                            } 

热点排行