单选按钮、列表视图和分组,天哪!

发布于 2024-08-20 03:52:50 字数 489 浏览 6 评论 0原文

我正在开发一个项目,需要将 RadioButton 放入 ListView 中,并让它们都具有相同的 GroupName。 (不能使用RadioButtonList,说来话长)。

无论如何,这似乎是 .NET 中的一个众所周知的错误,因此我实现了此处找到的解决方案:
ASP.NET RadioButton 弄乱了名称(组名)

工作完美,但有一个小错误,毫无疑问它会回来咬我。如果我单击一个单选按钮,然后在 javascript 函数仍在运行且尚未完成时单击另一个单选按钮;我可以在同一组中选择 2 个单选按钮。

有什么想法吗?如果您需要澄清;给我留言,我会更新我的帖子。

编辑:我相信我在这里解决了自己的问题。请参阅下面我发布的答案。

I've got a project I'm working on where I need to put a RadioButton inside a ListView, and have them all have the same GroupName. (can't use RadioButtonList, long story).

Anyway, this seems to be a well known bug in .NET, so I've implemented the solution found here:
ASP.NET RadioButton messing with the name (groupname)

This works perfectly, but with one small bug which undoubtedly will come back to bite me. If I click one radio button, and then click another while the javascript function is still running and has not completed; I can get 2 radiobuttons in the same group selected.

Any ideas? If you need clarification; leave me a comment and I'll update my post.

EDIT: I believe I fixed my own issue here. See my posted answer below.

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

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

发布评论

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

评论(2

挥剑断情 2024-08-27 03:52:50

像这样的东西会起作用吗?

if (!rbClicked)
    rbClicked=True;
    runJavascript();
    rbClicked=False;

我承认我没有做很多研究,但这就是我通常解决此类问题的方式。

Would something like this work?

if (!rbClicked)
    rbClicked=True;
    runJavascript();
    rbClicked=False;

I admit I didn't do alot of research, but this is how I've usually solved this type of problem.

小兔几 2024-08-27 03:52:50

原来我在这里被误导了。

稍后在页面生命周期中;我添加了一个额外的 onclick 事件来覆盖以前的值。

在以下示例中:

var radiobutton = new System.Web.UI.WebControls.RadioButton();
radiobutton.Attributes.Add("onclick", "test1");
radiobutton.Attributes.Add("onclick", "test2");

onlcick 属性设置为“test2”; test1 丢失。

我最终通过附加到现有属性来修复它;见下文:

radiobutton.Attributes["onclick"] = string.Format("{0};{1}", radiobutton.Attributes["onclick"], "My Second Script");

这可以正常工作。

Turns out I was misguided here.

Later on in the page lifecycle; I was adding an additional onclick event that was overwriting the previous value.

In the following example:

var radiobutton = new System.Web.UI.WebControls.RadioButton();
radiobutton.Attributes.Add("onclick", "test1");
radiobutton.Attributes.Add("onclick", "test2");

the onlcick attribute is set to "test2"; test1 is lost.

I ended up fixing it by appending to the existing attribute; see below:

radiobutton.Attributes["onclick"] = string.Format("{0};{1}", radiobutton.Attributes["onclick"], "My Second Script");

This works without issue.

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