RadioButtonList OnSelectedIndexChanged

时间:2011-08-08 14:41:30

标签: c# asp.net event-handling radiobuttonlist

我正在寻找处理在ASP.net RadioButtonList(后面的C#代码)上选择的索引更改的最佳方法。我有3个列表项。对于第一个,我希望它在页面上显示隐藏的asp:文本框,而另外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除了显示文本框之外什么都不做,但也不起作用。

任何帮助表示赞赏!谢谢大家。

2 个答案:

答案 0 :(得分:28)

在RadioButtonList上,将AutoPostBack属性设置为true。

答案 1 :(得分:1)

看看这可能会有所帮助。如果在单选按钮上启用,我建议关闭autopostback,在客户端使用jquery完成所有操作。

示例:

Using jQuery, hide a textbox if a radio button is selected