0

当我使用以下代码以编程方式创建单选按钮列表时

<%@ 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>

如果你仔细看,所有控件的“名称”属性都为零,所以我不能为不同的两个单选按钮列表选择两个选项。我只能选择一个。

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

4

1 回答 1

0

改用 CheckBoxList - 根据设计,当需要多个选择时使用它。

编辑

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

<%@ 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>
于 2013-07-13T15:47:42.007 回答