【发布时间】:2023-03-06 23:01:01
【问题描述】:
我现在有一个单选按钮列表,其中包含 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);
}
}
但在这一切之后,selectedindexchanged 事件不会触发。我尝试将单选按钮列表和页面的 EnableViewState 属性设置为 true。我还尝试通过 aspx 代码将第一项设置为选中,而不是这样做它在页面加载时,但没有任何效果。应该做什么?这是 selectedindexchanged 事件:
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
第一个项目被选中,没有问题,但是当我尝试选择第二个元素时,除了回发之外什么都没有发生。 单选按钮似乎有问题。我尝试在页面上添加两个单选按钮以查看它们是否有效。即使是简单的单选按钮也无法响应选择。但是当我添加复选框时,它们可以正常工作。
【问题讨论】:
-
事件
RadioButtonList1_SelectedIndexChanged在哪里? -
@Ganesh_Devlekar 目前我刚刚将 selectedindexchanged 事件保留为空白。如果您希望看到它,我会将其添加到我的问题中。
-
不是默认选择列表中的第一个吗?
-
您是否尝试在加载事件时触发 selectedindexchange?如果是,那么在
RadioButtonList1.Items[0].Selected = true;之后执行RadioButtonList1_SelectedIndexChanged(this, new EventArgs()) -
没有@Avijit,我只是将第一个元素设置为在页面加载事件中选择。那里没有问题,但是当我尝试选择第二个项目时,没有事件触发,只有回发发生。
标签: c# asp.net radiobuttonlist