单选按钮和下拉列表之间的依赖关系
本文关键字:依赖 关系 之间 下拉列表 单选按钮 | 更新日期: 2023-09-27 18:09:51
我有这样的代码
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton1.Checked)
{
RadioButton2.Checked = false;
DropDownList1.Enabled = true;
}
if (!RadioButton1.Checked)
{
RadioButton2.Checked = true;
}
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton2.Checked)
{
RadioButton1.Checked = false;
DropDownList1.Enabled = false;
}
if (!RadioButton1.Checked)
{
RadioButton1.Checked = true;
}
}
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="BookStore" DataTextField="Name" DataValueField="Name" Height="51px" Width="300px" DataMember="DefaultView">
</asp:DropDownList>
<asp:SqlDataSource ID="BookStore" runat="server" ConnectionString="<%$ ConnectionStrings:BookStoreConnectionString %>" SelectCommand="SELECT [Name] FROM [Books] ORDER BY [Name]"></asp:SqlDataSource>
<p>
<asp:RadioButton ID="RadioButton1" runat="server" Checked="True" GroupName="1" OnCheckedChanged="RadioButton1_CheckedChanged" />
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="1" OnCheckedChanged="RadioButton2_CheckedChanged" />
</p>
但是当我点击单选按钮时,下拉列表总是启用的。当我在webforms应用程序(不是asp.net)中编写此功能时,其工作正确。这种不正常工作的原因是什么?
可能是RadioButton2_CheckedChanged
事件在第二if条件下的问题,这应该检查RadioButton2.Checked
而不是RadioButton1.Checked
。而不是检查这个条件,如果你把else
仍然这将工作,不需要再次检查。
if (!RadioButton2.Checked)
{
RadioButton1.Checked = true;
}