简体   繁体   中英

How do I get the RadioButtonList_SelectedIndexChanged event handler to execute after the first execution of that event in asp.net C#?

I have a RadioButtonList with two RadioButtons and when I click on a radio button, it enables a DropDownList for that option.

Here's my HTML code.

<table>
    <tr>
        <td class="auto-style3">
        </td>
        <td style="font-size:medium" class="auto-style3">
           <asp:RadioButtonList ID="Database_Type" runat="server" Height="99px" OnSelectedIndexChanged="Database_Type_SelectedIndexChanged" AutoPostBack="True">
                                <asp:ListItem Text="Standard" Value="1"></asp:ListItem>
                                <asp:ListItem Text="Custom" Value="2"></asp:ListItem>
           </asp:RadioButtonList>
        </td>
        <td>
           &nbsp;&nbsp;</td>
        <td style="font-size:medium" class="auto-style3">
            <asp:DropDownList ID="StandardAircraftList" runat="server"></asp:DropDownList>
            <br />
            <br />
            <asp:DropDownList ID="CustomAircraftList" runat="server"></asp:DropDownList>
        </td>
    </tr>
</table>

Here's my C# code.

protected void Database_Type_SelectedIndexChanged(object sender, EventArgs e)
{
    if (Database_Type.SelectedItem.Value == "1")
    {
        StandardAircraftList.Enabled = true;
        CustomAircraftList.Enabled = false;
        StandardAircraftList.BackColor = System.Drawing.Color.White;
        CustomAircraftList.BackColor = System.Drawing.Color.Gray;
    }
    if (Database_Type.SelectedItem.Value == "2")
    {
        CustomAircraftList.Enabled = true;
        StandardAircraftList.Enabled = false;
        CustomAircraftList.BackColor = System.Drawing.Color.White;
        StandardAircraftList.BackColor = System.Drawing.Color.Gray;
    }
}

Please clarify what your expected behaviour is and how the behaviour is right now. The event is wired up correctly and gets executed upon selection of one of the options.

If by 'first execution of that event' you actually mean the first execution of the code behind in general and you're missing a 'default' behaviour, you'll need to implement a 'default state' for the radio buttons aswell as the dropdownlists, eg

<asp:ListItem Text="Standard" Value="1" Selected="True"></asp:ListItem>

and accordingly

StandardAircraftList.Enabled = true;
CustomAircraftList.Enabled = false;
StandardAircraftList.BackColor = System.Drawing.Color.White;
CustomAircraftList.BackColor = System.Drawing.Color.Gray;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM