单选按钮列表 OnSelectedIndexChanged
我正在寻找处理 ASP.net RadioButtonList (后面的 C# 代码)上选择的索引更改的最佳方法。我有 3 个列表项。对于第一个,我希望它在页面上显示隐藏的 asp:textbox,而其他 2 个将隐藏文本框。
//asp.net side
<asp:RadioButtonList ID="_indicatorAckType" runat="server" RepeatDirection="Horizontal"
enabled="true" OnSelectedIndexChanged="onAckTypeChanged">
<asp:ListItem Text="None" />
<asp:ListItem Text="SHOW" />
<asp:ListItem Text="HIDE" />
</asp:RadioButtonList>
//code behind
protected void onAckTypeChanged(object sender, EventArgs e)
{
if (_indicatorAckType.SelectedItem.Text == "SHOW")
_myTextboxID.Visible = true;
else
_myTextboxID.Visible = false;
}
我最初尝试使用 onclick 事件处理程序,但有人告诉我 ListItem 不能将 onclick 事件与单选按钮项一起使用。我在这里做错了什么?这不会引发任何错误或有任何明显的问题。我尝试让 onSelectedIndexChanged 除了显示文本框之外什么也不做,但这也不起作用。
任何帮助表示赞赏!谢谢大家。
I'm looking for the best way to handle a change of index being selected on a ASP.net RadioButtonList (C# code behind). I have 3 list items. For the first one, I want it to show a hidden asp:textbox on the page, whereas the other 2 will hide the textbox.
//asp.net side
<asp:RadioButtonList ID="_indicatorAckType" runat="server" RepeatDirection="Horizontal"
enabled="true" OnSelectedIndexChanged="onAckTypeChanged">
<asp:ListItem Text="None" />
<asp:ListItem Text="SHOW" />
<asp:ListItem Text="HIDE" />
</asp:RadioButtonList>
//code behind
protected void onAckTypeChanged(object sender, EventArgs e)
{
if (_indicatorAckType.SelectedItem.Text == "SHOW")
_myTextboxID.Visible = true;
else
_myTextboxID.Visible = false;
}
I initially tried using onclick event handlers, but I've been told that ListItem's cannot use onclick events with radio button items. What am I doing wrong here? This does not throw any errors or have any visibly obvious problems. I have tried making onSelectedIndexChanged do nothing except show the textbox and that doesn't work either.
Any help is appreciated! Thanks everyone.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 RadioButtonList 上,将
AutoPostBack
属性设置为 true。On the RadioButtonList, set the
AutoPostBack
attribute to true.看看这个可能会有帮助。我建议关闭自动回发(如果在单选按钮上启用),使用 jquery 在客户端执行所有操作。
示例:
使用 jQuery,隐藏文本框,如果单选按钮被选中
Have a look at this it might help. And I suggest to turn off autopostback if enabled on radio button do it all on client side using jquery.
example:
Using jQuery, hide a textbox if a radio button is selected