asp.net以编程方式生成radiobuttonlist

时间:2013-07-13 15:39:57

标签: asp.net radiobuttonlist listitem

当我使用下面的代码

以编程方式创建radiobuttonlist时
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RadioButtonList.Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
    <% for (int i = 0; i < 2; i++)
       {
           anketsonucu.ID = i.ToString();  
    %>
    <div>
        <br />
        <label>
            <span class="questionicon">>></span> Deneme</label>
        <asp:RadioButtonList ID="anketsonucu" runat="server" RepeatDirection="Horizontal"
            CssClass="anketsonucu">
            <asp:ListItem Text="Çok Kötü" Value="1"></asp:ListItem>
            <asp:ListItem Text="Kötü" Value="2"></asp:ListItem>
            <asp:ListItem Text="Orta" Value="3"></asp:ListItem>
            <asp:ListItem Text="İyi" Value="4"></asp:ListItem>
            <asp:ListItem Text="Çok İyi" Value="5"></asp:ListItem>
        </asp:RadioButtonList>
    </div>
    <%
       }
    %>
</div>
</form>
</body>
</html>

它给了我这个页面来源     

    <div>
        <br />
        <label>
            <span class="questionicon">>></span> Deneme</label>
        <table id="0" class="anketsonucu">
<tr>
    <td><input id="0_0" type="radio" name="0" value="1" />
<label for="0_0">Çok Kötü</label></td><td><input id="0_1" type="radio" name="0" value="2" />
<label for="0_1">Kötü</label></td><td><input id="0_2" type="radio" name="0" value="3" />
<label for="0_2">Orta</label></td><td><input id="0_3" type="radio" name="0" value="4" />
<label for="0_3">İyi</label></td><td><input id="0_4" type="radio" name="0" value="5" />
<label for="0_4">Çok İyi</label></td>
</tr>
</table>
    </div>

    <div>
        <br />
        <label>
            <span class="questionicon">>></span> Deneme</label>
        <table id="1" class="anketsonucu">
<tr>
    <td><input id="1_0" type="radio" name="0" value="1" /><label for="1_0">Çok Kötü</label></td><td>
<input id="1_1" type="radio" name="0" value="2" /><label for="1_1">Kötü</label></td><td>
 <input id="1_2" type="radio" name="0" value="3" /><label for="1_2">Orta</label></td><td>
 <input id="1_3" type="radio" name="0" value="4" /><label for="1_3">İyi</label></td><td>
<input id="1_4" type="radio" name="0" value="5" /><label for="1_4">Çok İyi</label></td>
</tr>
</table>
    </div>

</div>

如果你仔细观察所有控件都有相同的“name”属性为零,所以我不能为不同的两个radiobuttonlist选择两个选项。我只能选一个。

如何更改单选按钮的名称属性,以便我可以同时为不同的radiobuttonlist选择两个单选按钮?

1 个答案:

答案 0 :(得分:0)

使用CheckBoxList - 通过设计,当需要多个选项时使用它。

修改

Gotcha - 改用它。你的循环只是引用了SAME RadioButtonList,因为它已经在页面上定义了。我刚刚删除了你的RadioButtonList,以编程方式添加它,添加了一个PANEL来将它/它们添加到页面中,并删除了不再需要的结束括号。

<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="sandbox._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <% for (int i = 0; i < 2; i++)
           {
               //anketsonucu.ID = i.ToString();
               RadioButtonList rbl = new RadioButtonList();
               rbl.ID = "anketsonucu" + i.ToString();
               rbl.RepeatDirection = RepeatDirection.Horizontal;
               rbl.CssClass = "anketsonucu";
               rbl.Items.Add(new ListItem("Çok Kötü", "1"));
               rbl.Items.Add(new ListItem("Kötü", "2"));
               rbl.Items.Add(new ListItem("Orta", "3"));
               rbl.Items.Add(new ListItem("İyi", "4"));
               rbl.Items.Add(new ListItem("Çok İyi", "5"));
               pnl.Controls.Add(rbl);
           }
        %>
        <div>
            <br />
            <label><span class="questionicon">>></span> Deneme</label>
            <asp:Panel ID="pnl" runat="server" />
        </div>
    </div>
    </form>
</body>
</html>