Javascript ASP.NET 单选按钮列表 onclientclick

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2724103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:38:52  来源:igfitidea点击:

ASP.NET radiobuttonlist onclientclick

asp.netjavascriptvb.net

提问by jaffa

I've noticed there is no OnClientClick() property for the radiobuttonlist in the ASP.NET control set. Is this a purposeful omissiong on Microsoft's part? Anyway, I've tried to add OnClick to the radio button list like so:

我注意到 ASP.NET 控件集中的 radiobuttonlist 没有 OnClientClick() 属性。这是微软有意的疏忽吗?无论如何,我已经尝试将 OnClick 添加到单选按钮列表中,如下所示:

For Each li As ListItem In rblSearch.Items
    li.Attributes.Add("OnClick", "javascript:alert('jon');")
Next

But alas, it doesn't work. I've even checked the source in firebug, and there is no javascript shown in the radiobuttonlist. Does anyone know how to get this very simple thing working? I'm using ASP.NET control adpaters so don't know if that has anything to do with it.

但可惜,它不起作用。我什至检查了 firebug 中的源代码,并且单选按钮列表中没有显示 javascript。有谁知道如何让这个非常简单的事情工作?我正在使用 ASP.NET 控件适配器,所以不知道这是否与它有关。

(I wish asp.net/javascript would just work out the box!!!)

(我希望 asp.net/javascript 能开箱即用!!!)

回答by Daniel Ballinger

I found I could just add a onclick attribute to the RadioButtonList and it would fire the javascript client side as expected.

我发现我可以向 RadioButtonList 添加一个 onclick 属性,它会按预期触发 javascript 客户端。

<asp:RadioButtonList ID="RadioButtonList1" runat="server" onclick="alert('RadioButtonListSelectChange');">
    <asp:ListItem Text="One" Value="1"/>
    <asp:ListItem Text="Two" Value="2"/>
</asp:RadioButtonList>

You could then write a client script that could determine the currently selected list item.

然后您可以编写一个客户端脚本来确定当前选择的列表项。

回答by Daniel Silva

The onclick attribute function very good in ListItem object. Try in this way:

ListItem对象中的onclick属性功能很好。以这种方式尝试:

li.Attributes.Add("onclick", "javascript:showDiv()")

That function to me.

那个功能给我。

回答by user1664043

Not really an answer but a few weird observations on the other suggestions. I'll preface this by saying this is a RadioButtonList in an ASP.Net 4.0 app with RepeatDirection=Vertical

不是真正的答案,而是对其他建议的一些奇怪观察。我先说这是一个 ASP.Net 4.0 应用程序中的 RadioButtonList,其 RepeatDirection=Vertical

1) Adding onclick='myJsFunction(this);' attributes to the ListItems worked fine for me exceptafter a different item on the page causes a post back (a button action for example), the attributes on the ListItems don't get renderedin the result of the postback. They get rendered to start, but not when there's a button action. Go figure.

1) 添加 onclick='myJsFunction(this);' ListItems 的属性对我来说工作正常,除非页面上的不同项目导致回发(例如按钮操作),ListItems 上的属性不会在回发结果中呈现。它们会被渲染以开始,但不会在有按钮操作时渲染。去搞清楚。

2) Apparently our legacy code was relying on some murky, less than canonical behavior on the RadioButtonList/ListItem that gets very muddy on postback. If you put an idattribute on a ListItem, ASP.Net 2.0+ will add a spantag around the radio button and put your id value there. If you add an onclickattribute on it, that will go on the radio button - at least when Page.IsPostBack == false.

2) 显然,我们的遗留代码依赖于 RadioButtonList/ListItem 上的一些模糊的、不规范的行为,这些行为在回发时变得非常混乱。如果您将id属性放在 ListItem 上,ASP.Net 2.0+ 将在单选按钮周围添加一个span标记并将您的 id 值放在那里。如果您在其上添加一个onclick属性,它将出现在单选按钮上 - 至少当 Page.IsPostBack == false 时。

<asp:RadioButtonList  ID="proceedChoice" runat="server" RepeatDirection="Vertical">
    <asp:ListItem id="Close_Case_Y" Value="Y" onclick="SelectResolveType(this);" Text="I want to close this case."  runat="server" />
    <asp:ListItem id="Close_Case_N" Value="N" onclick="SelectResolveType(this);" Text="I want to cancel this and start over."  runat="server" />
</asp:RadioButtonList>

renders as

呈现为

<table id="...proceedChoice" border="0">
<tr>
    <td><span id="Close_Case_Y"><input id="..._proceedChoice_0" type="radio" value="Y" onclick="SelectResolveType(this);" /><label for="..._proceedChoice_0">I want to close this case.</label></span></td>
</tr><tr>
    <td><span id="Close_Case_N"><input id="..._proceedChoice_1" type="radio" value="N" onclick="SelectResolveType(this);" /><label for="..._proceedChoice_1">I want to cancel this case and start over.</label></span></td>
</tr>
</table>

After a postback from some other button, though the same template renders as

从其他按钮回发后,虽然相同的模板呈现为

<table id="...proceedChoice" border="0">
<tr>
    <td><input id="..._proceedChoice_0" type="radio" value="Y" /><label for="..._proceedChoice_0">I want to close this case.</label></td>
</tr><tr>
    <td><input id="..._proceedChoice_1" type="radio" value="N" /><label for="..._proceedChoice_1">I want to cancel this case and start over.</label></td>
</tr>
</table>

But here's where it gets really weird: if you tack on other attributes to ListItem other than id and onclick, just thoseattributes get preserved. For example if I add hidden="hidden" to one of the ListItems, it gets rendered like this after postback

但这里真的很奇怪:如果您将除 id 和 onclick 以外的其他属性附加到 ListItem,则只会保留这些属性。例如,如果我将 hidden="hidden" 添加到 ListItems 之一,它会在回发后呈现如下

<table id="...proceedChoice" border="0">
<tr>
    <td><span hidden="hidden"><input id="..._proceedChoice_0" type="radio" value="Y" /><label for="..._proceedChoice_0">I want to close this case.</label></span></td>
</tr><tr>
    <td><input id="..._proceedChoice_1" type="radio" value="N" /><label for="..._proceedChoice_1">I want to cancel this case and start over.</label></td>
</tr>
</table>

回答by Eric

            foreach (PlaceType t in query.ToList())
        {

            ListItem li = new ListItem(@"<img src=""" + t.ImageRelativePath + @"""/><br/>" + t.PlaceTypeText, t.PlaceTypeID.ToString());
            li.Attributes.Add("onclick", "javascript:placeTypeChange('" + t.ImageRelativePath + "')");
            rblPlaceTypes.Items.Add(li);
        }

So in there I'm

所以在那里我

a) Iterating over the results of a EF query. b) Creating a new list item, and adding it to my radio button list (rblPlaceTypes). c) The constructor is putting an image in so that displays. d) the li.Attributs.Add is putting the javascript wireup in.

a) 迭代 EF 查询的结果。b) 创建一个新的列表项,并将其添加到我的单选按钮列表 (rblPlaceTypes)。c) 构造函数将图像放入以便显示。d) li.Attributs.Add 正在放入 javascript 接线。

回答by Dan

Because it's a list control, there isn't a OnClientClick event. Either use a postback (SelectedIndexChange) or write javascript to grab the click for each radio button.

因为它是一个列表控件,所以没有 OnClientClick 事件。使用回发 (SelectedIndexChange) 或编写 javascript 来获取每个单选按钮的点击。