我现在有一个包含 2 个项目的单选按钮列表。下面是 aspx 代码:
<asp:RadioButtonList ID="RadioButtonList1" runat="server" Font-Bold="True"
Height="52px" Width="181px" AutoPostBack="True" EnableTheming="True"
EnableViewState="true" onselectedindexchanged="RadioButtonList1_SelectedIndexChanged">
<asp:ListItem Value="Head of family "></asp:ListItem>
<asp:ListItem Value="Show all">All Family Members</asp:ListItem>
</asp:RadioButtonList>
在页面加载时第一次加载页面时,我通过以下代码将第一个单选按钮项设置为所选项:
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
RadioButtonList1.Items[0].Selected = true;
}
}
catch (Exception ce)
{
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + ce.Message + "');", true);
}
}
但是在所有这些之后,选定的索引更改事件不会触发。我尝试将单选按钮列表和页面的 EnableViewState 属性设置为 true。我还尝试将第一项设置为通过 aspx 代码选择,而不是在页面加载时执行此操作,但没有任何效果。应该怎么做?这是选定的索引更改事件:
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
第一项被选中,没有问题,但是当我尝试选择第二个元素时,除了回发之外什么也没发生。单选按钮似乎有问题。我尝试在页面上添加两个单选按钮,看看它们是否有效。即使是简单的单选按钮也不会响应选择。但是,当我添加复选框时,它们工作正常。
如果在代码隐藏中选择该项,则不会触发该事件。
从 MSDN:
当从列表控件中选择的内容在发布到服务器之间发生更改时,将引发 SelectedIndexChanged 事件。
更多信息请点击此处:
ListControl.OnSelectedIndexChanged Method
从控件中删除onselectedindexchanged="RadioButtonList1_SelectedIndexChanged"
RadioButtonList
并尝试添加
RadioButtonList1.SelectedIndexChanged += new EventHandler(RadioButtonList1_SelectedIndexChanged);
在Page_Load
之外
if (!IsPostBack)
{
}
块。看看这是否有效。