【问题标题】:RadioButtonList ListItem OnClick event?RadioButtonList ListItem OnClick 事件?
【发布时间】:2014-06-10 02:37:17
【问题描述】:

我有一个用户控件,上面有一个 RadioButtonList。

<asp:RadioButtonList ID="rblMyOptions" runat="server" RepeatLayout="Flow"
RepeatDirection="Horizontal"  >
    <asp:ListItem Text="Yes" Value="Y" onClick="getCheckedRadio(true);" />
    <asp:ListItem Text="No" Value="N" onClick="getCheckedRadio(false);" />
</asp:RadioButtonList>

如您所见,我最初设置了一个 onClick 事件来激活 JavaScript 函数,该代码在页面上运行良好,但自从移动到用户控件后,它就停止调用它了。

代码有以下消息:“验证(ASP.Net):属性'onClick'不是元素'ListItem'的有效属性。

无论如何,我可以有一个 onClick 样式事件来调用我的函数吗?我已经看到一些示例不断迭代项目,然后确定哪个被选中,但是我有很多这些单选按钮,所以如果可能的话,我不想使用它。

【问题讨论】:

  • 你在用 JQuery 吗?
  • 如有必要,我可以这样做,在某些部分它确实使用 JQuery(用于其他功能)。只是这个方法没有激活。

标签: javascript asp.net


【解决方案1】:

考虑把这个 JQuery 或一些变体放在你的主窗体上。

//On document ready
$(function() {
    $("input[type='radio']").on('click', function(e) {
       getCheckedRadio($(this).attr("name"), $(this).val(), this.checked);
    });
});

//External function to handle all radio selections
function getCheckedRadio(group, item, value) {
    $(".group-label").text(group);
    $(".item-label").text(item);
    $(".item-value").text(value);
}

然后在你的部分:

<asp:RadioButtonList ID="rblMyOptions" runat="server" RepeatLayout="Flow" RepeatDirection="Horizontal">
    <asp:ListItem Text="Yes" Value="Y" />
    <asp:ListItem Text="No" Value="N" />
</asp:RadioButtonList>

这应该处理所有的单选按钮(或者如果你改变了它,至少是你用 JQuery 定位的那些)。

我在这里有一个工作示例(没有 .NET 代码)来演示该原理。

http://jsfiddle.net/xDaevax/UwEN5/

编辑:这基本上是幕后发生的事情。

我不确定你对这件事有多陌生,所以我会在这里详细介绍一下,如果你已经掌握了其中一些事情,我深表歉意。

当您使用 .NET 控件(asp:DropDownList、asp:RadioButtonList、asp:ListItem)时,这些都是在将页面发送到浏览器时生成的服务器端表示。您在 .aspx 页面中看到的代码不一定是标记发送到浏览器的方式。

例如下面的代码:

<asp:Label ID="_CtlMyLabel" runat="server" Text="Label" />

将在浏览器 HTML 中呈现为:

<span id="_CtlMyLabel">Label</span>

如果你将它包装在一个面板中:

<asp:Panel ID="_CtlWrapperPanel" runat="server" ClientIDMode="Inherit">
    <asp:Label ID="_CtlMyLabel" runat="server" Text="Label" ClientIDMode="Inherit" />
</asp:Panel>

你会得到:

<div id="_CtlWrapperPanel">
    <span id="_CtlMyLabel">Label</span>
</div>

说到一个RadioButtonList,如下asp:RadioButtonList:

<asp:RadioButtonList ID="rblMyOptions" runat="server" RepeatLayout="Flow" RepeatDirection="Horizontal">
    <asp:ListItem Text="Yes" Value="Y" />
    <asp:ListItem Text="No" Value="N" />
</asp:RadioButtonList>

将生成:

<span id="rblMyOptions">
    <input id="rblMyOptions_0" type="radio" name="rblMyOptions" value="Y" />
    <label for="rblMyOptions_0">Yes</label>
    <input id="rblMyOptions_1" type="radio" name="rblMyOptions" value="N" />
    <label for="rblMyOptions_1">No</label>
</span>

当您在 ListItem 上说 OnClick 时,.NET 认为您指的是您创建的名为 rblMyOptions_Click 的服务器端函数(它将执行回发),但由于您希望这发生在客户端上,所以我的策略是建议解决它是使用JQuery绑定到每个单选按钮的点击事件,因为“单选按钮列表”实际上并不存在于HTML中。

您可以查看此问题/答案以获取更多信息: ASP.NET radiobuttonlist onclientclick

通过使用 JQuery 来定位任何 input[type='radio'],我们将 javascript 绑定到任何单选按钮输入(或者我们也可以更加严格)。

【讨论】:

  • 嗨 xDaevax ListItem 的输入类型是什么?您的 jsfiddle 示例有效,但我的代码没有得到任何响应。我输入了 alert('Group item: ' + group.toString());但它似乎没有开火。
  • RadioButtonList 控件只呈现一系列同名的单选按钮输入。在我的示例中,我将它们命名为好像它们是由两个不同的 RadioButtonList 控件生成的一样。 ListItem 是与复选框列表、下拉列表和单选按钮列表一起使用的通用 WebForms 控件。通常将 OnClientClick 放在控件本身而不是列表项上会更好。我分享的 JQuery 方法只是实现它的另一种方式。
  • 好吧,我不完全确定我遵循(我是新来的)。标记与帖子中的示例相同,是否有任何特定原因导致已检查事件未触发警报消息? OnClientClick 似乎不是一个有效的属性,所以我不能添加它。
  • 我明白了,我会在这里快速加载一个工作室解决方案并详细更新我的答案。
  • @MattR 我已经更新了我的答案,提供了更多细节,希望有助于澄清。如果没有,我们可以随时将此对话移至聊天。
【解决方案2】:

这似乎是一个已知的错误

参考这个document(转到“为什么属性不能应用于列表控件的列表项”部分)

核心应该在此代码中以修复错误

    public void RenderItem(System.Web.UI.WebControls.ListItemType itemType, 
                           int repeatIndex, RepeatInfo repeatInfo, 
                           HtmlTextWriter writer)
    {
        controlToRepeat.ID = repeatIndex.ToString(NumberFormatInfo.InvariantInfo);
        controlToRepeat.Text = this.Items[repeatIndex].Text;
        controlToRepeat.TextAlign = this.TextAlign;
        controlToRepeat.Checked = this.Items[repeatIndex].Selected;
        controlToRepeat.Enabled = this.Enabled; 

        controlToRepeat.Attributes.Clear();
        foreach (string key in Items[repeatIndex].Attributes.Keys)
            controlToRepeat.Attributes.Add(key, Items[repeatIndex].Attributes[key]);

        controlToRepeat.RenderControl(writer);

}

【讨论】:

  • 这将提供所需的 onClick 属性?因为我不确定在不知道特定属性名称以及 repeatIndex 是什么的情况下会如何。我会读这篇文章
  • 阅读我提到的部分中的文档,您将能够解决这个问题。
  • 我是,这有点偏离“在 ASP.NET 1.x 中”,因为我认为这不是当前的问题。
  • 我在最近的一篇博客文章中找到了这个链接,所以我想这仍然是一个有效的解决方案。如果您愿意,请尝试让我知道它是否可以解决您的问题。否则我很抱歉不能帮助你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-07
  • 1970-01-01
  • 1970-01-01
  • 2012-03-13
  • 2023-03-11
相关资源
最近更新 更多