asp.net单选按钮组(asp.net radio button grouping)

2019-07-03 19:06发布

我目前有开关按钮和分组的问题。 我有一个中继器控制范围内的ASP单选按钮。 我有组名称属性设置为“客户”。 页面加载时,单选按钮不进行分组。 代替的ID字段被设定为所述组的名称,它是设置的单选按钮的值的字段。 我知道,我已经尝试设置单选按钮一个中继器的控制之外,并有同样的问题。 这里发生了什么?

ASPX

<asp:Repeater ID="repCustomers" runat="server">
    <HeaderTemplate>
        <table class="tableDefault" cellpadding="0" cellspacing="0" border="0" style="width: 383px; border: 0px !important">
            <tr>
                <th>&nbsp;</th>
                <th>Cust. No.</th>
                <th>Cust. Name</th>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
            <tr>
                <td>
                    <asp:RadioButton ID="radCustomer" GroupName="Customer" runat="server" ValidationGroup="Customer" ToolTip='<%#Eval("CustomerNumber") %>' />
                </td>
                <td><%#Eval("CustomerNumber")%></td>
                <td><%#Eval("Name") %></td>
            </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>

输出HTML

<table class="tableDefault" cellpadding="0" cellspacing="0" border="0" style="width: 383px; border: 0px !important">
    <tr>
        <th>&nbsp;</th>
        <th>Cust. No.</th>
        <th>Cust. Name</th>
    </tr>

    <tr>
        <td>
            <span title="111111"><input id="ctl00_PrimaryContent_repCustomers_ctl01_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl01$Customer" value="radCustomer" /></span>
        </td>
        <td>111111</td>
        <td>Jeremy's Test</td>
    </tr>

    <tr>
        <td>
            <span title="222222"><input id="ctl00_PrimaryContent_repCustomers_ctl02_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl02$Customer" value="radCustomer" /></span>
        </td>
        <td>222222</td>
        <td>My Test</td>
    </tr>

    <tr>
        <td>
            <span title="333333"><input id="ctl00_PrimaryContent_repCustomers_ctl03_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl03$Customer" value="radCustomer" /></span>
        </td>
        <td>333333</td>
        <td>Jim Bob's BBQ</td>
    </tr>

    <tr>
        <td>
            <span title="444444"><input id="ctl00_PrimaryContent_repCustomers_ctl04_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl04$Customer" value="radCustomer" /></span>
        </td>
        <td>444444</td>
        <td>New Hope Hamburgers</td>
    </tr>

    <tr>
        <td>
            <span title="555555"><input id="ctl00_PrimaryContent_repCustomers_ctl05_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl05$Customer" value="radCustomer" /></span>
        </td>
        <td>555555</td>
        <td>Pied Piper Pizza</td>
    </tr>

    <tr>
        <td>
            <span title="666666"><input id="ctl00_PrimaryContent_repCustomers_ctl06_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl06$Customer" value="radCustomer" /></span>
        </td>
        <td>666666</td>
        <td>Sandy's Subs</td>
    </tr>

    <tr>
        <td>
            <span title="777777"><input id="ctl00_PrimaryContent_repCustomers_ctl07_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl07$Customer" value="radCustomer" /></span>
        </td>
        <td>777777</td>
        <td>Leonard's Lambchops</td>
    </tr>

    <tr>
        <td>
            <span title="888888"><input id="ctl00_PrimaryContent_repCustomers_ctl08_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl08$Customer" value="radCustomer" /></span>
        </td>
        <td>888888</td>
        <td>Dave's Diamond Deli</td>
    </tr>

    <tr>
        <td>
            <span title="999999"><input id="ctl00_PrimaryContent_repCustomers_ctl09_radCustomer" type="radio" name="ctl00$PrimaryContent$repCustomers$ctl09$Customer" value="radCustomer" /></span>
        </td>
        <td>999999</td>
        <td>Ernie's Eatery</td>
    </tr>

</table>

Answer 1:

我终于得到了解决此通过创建一个普通的单选按钮,使用服务器端EVAL设定值。

<input type="radio" name="radCustomer" value='<%#Eval("CustomerNumber") %>' />

现在,当应用程序执行回发,予检查的Request.Form [“radCustomer”]的值。 这完美的作品。



Answer 2:

不幸的是,这是一个很好的已知问题中继器内的单选按钮 。 你的一个唯一的选择是创建从单选按钮类派生的自定义服务器控件和覆盖它如何呈现。

编辑 :这是什么样的派生类可能看起来像一个示例:

public class MyRadioButton : RadioButton
{
    protected override void Render(HtmlTextWriter writer)
    {
        writer.Write("<input id=\"" + base.ClientID + "\" ");
        writer.Write("type=\"radio\" ");
        writer.Write("name=\"" + base.ID + "\" ");
        writer.Write("value=\"" + base.ID + "\" />");
        writer.Write("<label for=\"" + base.ClientID + "\">");
        writer.Write(base.Text);
        writer.Write("</label>");
    }
}


Answer 3:

我固定它在JavaScript

$(document).ready(function () {
        $("#divWithGridViewOrRepeater input:radio").attr("name", "yourGroupName");
    }); 


Answer 4:

我有同样的问题。 我使用的文字作为占位符呈现单选按钮onItemCreated事件。

ASP.Net

<asp:Repeater ID="rpt" runat="server" OnItemCreated="rpt_OnItemCreated">
    <ItemTemplate>
        <asp:Literal ID="lit" runat="server"></asp:Literal>
    </ItemTemplate>
</asp:Repeater>

C#

protected void rpt_OnItemCreated(object sender, RepeaterItemEventArgs e) {
    Literal lit = (Literal)e.Item.FindControl("lit");
    lit.Text = "<input type=\"radio\" name=\"myGroup\">";
}


Answer 5:

我不得不通过r3dsky上面贴的回答稍微修改。

下面是我工作:

$(document).ready(function () {
        $(".divWithGridViewOrRepeater input:radio").attr("name", "yourGroupName");
    }); 


Answer 6:

我将由我的单选按钮值=“<%#的eval(‘CUSTOMERNUMBER’)%>”将值相加后开始。



Answer 7:

我做了我的单选按钮都自动回设置为true,然后在事件处理程序中设置的所有其他的单选按钮被选中。

并不理想,但我需要大量的控制权,可见性和启用单选按钮的属性,它似乎更容易让ASP.NET控件,而不是诉诸于客户端脚本。



Answer 8:

这是一个众所周知的缺陷与ASP.NET直放站使用单选按钮: 在我看来,这里最好的解决办法



Answer 9:

我这样做:

$("input:radio").attr("name", $("input:radio").first().attr("name"));

为什么? 因为如果你更换名称属性你想要的任何字符串,你会得到一个“未找到错误”。 所以,你需要得到第一个单选按钮的名称,并以该名称命名所有的人。 它像一个沙姆沙伊赫;)



Answer 10:

我的解决方案,类似于其他:

<input id="ctlRadio" runat="server" type="radio" data-fixgroupbug="1" >

// Fixes this ASP.NET bug: if radio input is inside repeater you can't set its name.
// Every input gets set different name by ASP.NET.
// They don't behave as a group. You can select multiple radios.
function fixRadiogroupBug()
{
    $('[type="radio"][data-fixgroupbug]').click(function () {
        $(this).siblings('[type="radio"]').prop('checked', false);
    });
}

$(document).ready(function () {
    fixRadiogroupBug();
});


文章来源: asp.net radio button grouping