radiobutton和下拉列表之间的依赖关系

时间:2016-09-30 10:10:41

标签: c# asp.net

我有这段代码

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>

但是当我点击radiobuttons时,下拉列表已经启用了。当我在webforms app(而不是asp.net)中编写这个函数时,它的工作正确。这可能是造成这种不正确工作的原因吗?

1 个答案:

答案 0 :(得分:0)

在第二条件RadioButton2_CheckedChanged事件中可能会出现问题,这应检查RadioButton2.Checked而不是RadioButton1.Checked。如果您将else置于静止状态,则无需再次检查,而不是检查此情况。

if (!RadioButton2.Checked)
{
    RadioButton1.Checked = true;
}