我注意到ASP.NET控件集中的radiobuttonlist没有OnClientClick()属性。这是微软有目的的遗漏吗? 无论如何,我试图将OnClick添加到单选按钮列表中,如下所示:
For Each li As ListItem In rblSearch.Items
li.Attributes.Add("OnClick", "javascript:alert('jon');")
Next
但是,唉,它不起作用。我甚至在firebug中检查了源代码,并且radiobuttonlist中没有显示javascript。有谁知道如何让这个非常简单的工作?我正在使用ASP.NET控件adpaters,因此不知道它是否与它有任何关系。
(我希望asp.net/javascript可以解决方案!!!)
答案 0 :(得分:26)
我发现我可以向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>
然后,您可以编写一个可以确定当前所选列表项的客户端脚本。
答案 1 :(得分:2)
onLlick属性在ListItem对象中非常好用。试试这种方式:
li.Attributes.Add("onclick", "javascript:showDiv()")
对我来说这个功能。
答案 2 :(得分:1)
不是一个答案,而是对其他建议的一些奇怪的观察。我将通过说这是ASP.Net 4.0应用程序中的RadioButtonList(重复方向=垂直
)来作为序言。1)添加onclick =&#39; myJsFunction(this);&#39; ListItems的属性对我来说很好除了在页面上的其他项目导致回发后(例如按钮操作),ListItems 上的属性不会被渲染在回发的结果中。它们被渲染开始,但不是在有按钮动作时。去图。
2)显然我们的遗留代码依赖于RadioButtonList / ListItem上的一些模糊的,不那么规范的行为,这些行为在回发时变得非常混乱。如果您在ListItem上放置 id 属性,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>
呈现为
<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>
从其他按钮回发后,虽然相同的模板呈现为
<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>
但是在这里它变得非常奇怪:如果你将其他属性添加到ListItem而不是id和onclick,只保留那些属性。例如,如果我添加隐藏=&#34;隐藏&#34;对于其中一个ListItem,它在回发后被渲染为
<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>
答案 3 :(得分:0)
因为它是一个列表控件,所以没有OnClientClick事件。使用回发(SelectedIndexChange)或编写javascript来获取每个单选按钮的单击。
答案 4 :(得分:0)
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);
}
所以我在那里
a)迭代EF查询的结果。 b)创建一个新的列表项,并将其添加到我的单选按钮列表(rblPlaceTypes)。 c)构造函数正在放入一个图像以便显示。 d)li.Attributs.Add将javascript连接放入。