【问题标题】:asp.net generating radiobuttonlist programmaticallyasp.net 以编程方式生成单选按钮列表
【发布时间】:2013-07-11 23:38:19
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: asp.net radiobuttonlist listitem


    【解决方案1】:

    改为使用 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>
    

    【讨论】:

    • 没有。我不想多选。我在同一页面上有两个单选按钮列表。但是这些单选按钮列表可以互相看到。例如,对于第一个问题,我有 5 个单选按钮,对于第二个问题,我有 5 个单选按钮。我想为第一个问题选择一个单选按钮,为第二个问题选择一个单选按钮。但我不能,因为 10 个单选按钮相互连接,我只能选择其中一个。
    猜你喜欢
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    相关资源
    最近更新 更多