RadioButtons 未在 ASP.NET 2.0 中继器中正确分组

发布于 2024-12-01 19:02:44 字数 629 浏览 3 评论 0原文

我有一个 ASP.NET 2.0 中的 Web 应用程序,其中需要一个高度自定义的网格。网格中的一列包含每行一个单选按钮。

我将其实现为 Repeater 控件,每个 ItemTemplate 中都有一个 div。问题在于单选按钮(ASP:RadioButton 标记)没有按照应有的方式进行分组;选择其中之一并不会取消选择其余的。我已经在它们上设置了 GroupName 属性,但我没有看到它通过 Firebug 在任何地方的 HTML 中呈现。谷歌搜索告诉我

<input id="{asp_garbage_naming}_ctl01_rbFoo" type="radio"
   name="ctl03$controlName$otherControlName$ctl01$name"
   value="rbHost" checked="checked" />

有办法让它工作吗?或者我是否必须自己提供单选按钮行为(javascript 等)?

I've got a web app in ASP.NET 2.0 in which I need to have a highly customized grid. One of the columns in the grid contains a radio button for each row.

I'm implementing it as a Repeater control, with a div in each ItemTemplate. The problem is that the radio buttons (ASP:RadioButton tags) are not being grouped like they should; selecting one of them doesn't deselect the rest. I've set the GroupName property on them already, but I don't see that being rendered in the HTML anywhere via Firebug. A google search tells me that the "name" attribute on <input type='radio> is what determines its group membership, but ASP is using that already as some kind of unique identifier. Each radio button looks something like this when rendered to HTML:

<input id="{asp_garbage_naming}_ctl01_rbFoo" type="radio"
   name="ctl03$controlName$otherControlName$ctl01$name"
   value="rbHost" checked="checked" />

Is there a way to make this work? Or am I going to have to provide radio button behavior on my own (javascript, etc)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怕倦 2024-12-08 19:02:44

一位同事遇到了这个问题并实现了一个 jQuery 解决方案。这是它的摘录:

这给了我想要的单选按钮功能,但它阻止我在回发时获取选定的单选按钮。所以我决定手动实现单选按钮功能。

var radios = $("input:radio");
radios.click(function() {
     radios.removeAttr('checked');
     $(this).attr('checked', 'checked');
     return true;
});

这给了我正确的功能,并且我仍然可以在回发时获取选定的单选按钮和文本框。可能不是最优雅的解决方案,但我找不到任何其他方法来做到这一点。

完整帖子:中继器问题中的单选按钮

A coworker ran across this issue and implemented a jQuery solution. Here's an excerpt of it:

That gave me the radio button functionality that I wanted, but it prevented me from getting the selected radio button on postback. So I decided to just implement the radio button functionality manually.

var radios = $("input:radio");
radios.click(function() {
     radios.removeAttr('checked');
     $(this).attr('checked', 'checked');
     return true;
});

Which gave me the correct functionality and I could still get the selected radio button and textbox on postback. Probably not the most elegant solution, but I couldn’t find any other way to do it.

Full post: Radio button within a repeater problem

水波映月 2024-12-08 19:02:44

在服务器端执行如下操作:

protected void rptRepeaterName_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    RadioButton rdbRadioButtonName = e.Item.FindControl("rdbRadioButtonName") as RadioButton;
    Repeater rptRepeaterName = sender as Repeater;
    rdbRadioButtonName.Attributes.Add("onclick", string.Format("return radioSelected('{0}', '{1}')", rptRepeaterName.ClientID, rdbRadioButtonName.ClientID));
}

在 javascript 中执行如下操作

function radioSelected(rptpricelevel, rdbPriceLevel)
{        
    for (cnt = 0; cnt<100; cnt++)
    {            
        var rdbId = rptpricelevel;
        if(cnt < 10)
        {
            rdbId = + '_ctl0' + cnt + '_rdbRadioButtonName';
        }
        else
        {
            rdbId = + '_ctl' + cnt + '_rdbRadioButtonName';
        }
        var rdb = document.getElementById(rdbId);
        if(rdb != null)
        {
            if(rdbId != rdbPriceLevel)
            {
                rdb.checked = false;
            }
        }
     }         
}

In the server side do as follows:

protected void rptRepeaterName_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    RadioButton rdbRadioButtonName = e.Item.FindControl("rdbRadioButtonName") as RadioButton;
    Repeater rptRepeaterName = sender as Repeater;
    rdbRadioButtonName.Attributes.Add("onclick", string.Format("return radioSelected('{0}', '{1}')", rptRepeaterName.ClientID, rdbRadioButtonName.ClientID));
}

In the javascript do as follows

function radioSelected(rptpricelevel, rdbPriceLevel)
{        
    for (cnt = 0; cnt<100; cnt++)
    {            
        var rdbId = rptpricelevel;
        if(cnt < 10)
        {
            rdbId = + '_ctl0' + cnt + '_rdbRadioButtonName';
        }
        else
        {
            rdbId = + '_ctl' + cnt + '_rdbRadioButtonName';
        }
        var rdb = document.getElementById(rdbId);
        if(rdb != null)
        {
            if(rdbId != rdbPriceLevel)
            {
                rdb.checked = false;
            }
        }
     }         
}
谈下烟灰 2024-12-08 19:02:44

问题?您是否需要这些单选按钮作为服务器控件(即您是否需要 runat=server 标签)?如果没有,您可以简单地在列上放置常规 html 单选按钮,并使用 <%#Eval("Property")%> 语法将任何属性绑定到它。只是一个想法

Question? Do you need these radio buttons to be server controls (i.e. do you need the runat=server tag)? If not, you could simply have regular html radio buttons on the column and bind any properties to it using the <%#Eval("Property")%> syntax. Just a thought

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文