Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.NET RadioButton列表OnClient单击_Asp.net_Javascript_Vb.net - Fatal编程技术网

ASP.NET RadioButton列表OnClient单击

ASP.NET RadioButton列表OnClient单击,asp.net,javascript,vb.net,Asp.net,Javascript,Vb.net,我注意到ASP.NET控件集中没有radiobuttonlist的OnClientClick()属性。这是微软故意的疏忽吗? 无论如何,我已经尝试将OnClick添加到单选按钮列表中,如下所示: For Each li As ListItem In rblSearch.Items li.Attributes.Add("OnClick", "javascript:alert('jon');") Next 但是,唉,它不起作用。我甚至在firebug中检查了源代码,并且radiobutto

我注意到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能够解决这个问题!!!)

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

我发现我可以在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>


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

在ListItem对象中,onclick属性函数非常好。试着这样做:

li.Attributes.Add("onclick", "javascript:showDiv()")
这个函数对我来说很有用。

foreach(query.ToList()中的PlaceType t)
            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);
        }
{ ListItem li=新的ListItem(@“
”+t.PlaceTypeText,t.PlaceTypeID.ToString()); Add(“onclick”,“javascript:placeTypeChange”(“+t.ImageRelativePath+”)”); rblPlaceTypes.Items.Add(li); }
所以我在里面

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

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

1) 添加onclick='myJsFunction(this);'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>

我想结案。
我想取消这个案子,重新开始。
从其他按钮回发后,虽然同一模板呈现为

<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>

我想结案。
我想取消这个案子,重新开始。
但这里是它变得非常奇怪的地方:如果您将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>

我想结案。
我想取消这个案子,重新开始。

向我们展示更多您的代码。您是将列表项绑定到后端的radiobuttonlist,还是在aspx中以声明方式绑定?阅读“为什么属性不能应用于列表控件的listitems”一节,不,它只是一个普通的旧静态列表。我认为下面的答案就是答案。我认为复选框列表属性根本不会被渲染。我在FireFox中尝试过,但发现onclick事件被触发了两次(大概每个列表项触发一次)。@EdGraham,我也有同样的经历,但我认为这与单击单选按钮的标签有关。该事件在单击无线电输入时仅触发一次,但在单击标签时触发两次。这使得这个答案几乎毫无用处。查找关于ListItems如何只在viewstate中隐藏文本和值的帖子,这就是为什么回发时属性会丢失的原因,这是有意义的,但我仍然感到困惑,为什么像hidden这样的属性会保留在回发时…还使用Reflector查找4guys RenderItem()覆盖建议现已纳入框架。