locked
Disable DropDownList4 if Approved is selected in DropDownList3. RRS feed

  • Question

  • User-1559458579 posted

    ASP.Net, C#, WebForms I would like to disable Dropdownlist4 if user makes selection of 'approved' in DropDownList3. I know this has to be done in the Code Behind but I don't know the syntax. Can someone provide?

      <tr>
                            <td>Approve this Request</td>
                            <td>
                                <asp:DropDownList ID="DropDownList3" runat="server" Width="250px">
                                    <asp:ListItem Value="0">Select</asp:ListItem>
                                    <asp:ListItem Value="approved">Approve</asp:ListItem>
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td>Deny this Request</td>
                            <td>
                                <asp:DropDownList ID="DropDownList4" runat="server" Width="250px">
                                    <asp:ListItem Value="0">Select</asp:ListItem>
                                    <asp:ListItem Value="approve">Deny</asp:ListItem>
                                </asp:DropDownList>
                            </td>
                        </tr>

    Tuesday, October 6, 2020 9:05 PM

Answers

  • User409696431 posted

    Assuming you want this to occur OnSelectedIndexChanged of DropDownList3, modify your markup to set autopostback to true and add a reference to that event.

    <asp:DropDownList ID="DropDownList3" runat="server" Width="250px" AutoPostBack="true" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged">
           <asp:ListItem Value="0">Select</asp:ListItem>
           <asp:ListItem Value="approved">Approve</asp:ListItem>
    </asp:DropDownList>

    In your code behind define the DropDownList3_SelectedIndexChanged function:

    protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
            {
                DropDownList dl4 = (DropDownList)FormView1.FindControl("DropDownList4");
                dl4.Enabled = false;
            }

    You also have to decide how you really want to handle this because if someone changes their mind, they can change Approved back to Select, but they can't select Deny.  And I assume you also want to handle the reverse event, selecting Deny. Perhaps you need to check the selected value and disable the other if it is Approve and enable the other it if it is Select.  In any case, wouldn't it make more sense to have one DropDownList with both Approve and Deny as options?  A DropDownList allows only one selection - no need to fuss with enabling and disabling other DropDownsLists to prevent both from being selected.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 6, 2020 11:55 PM

All replies

  • User409696431 posted

    Could you please edit your post and put plan text in the box unless it's markup or code, and use the code-insert icon- {i} - for markup or code.  It's quite unreadable as it is now.

    Tuesday, October 6, 2020 9:59 PM
  • User-1559458579 posted

    Yes, I'll edit

    Tuesday, October 6, 2020 10:33 PM
  • User-1559458579 posted

    PLEASE NOTE! THE DROPDOWNLIST ARE INSIDE A FORMVIEW.

    Tuesday, October 6, 2020 10:34 PM
  • User409696431 posted

    Assuming you want this to occur OnSelectedIndexChanged of DropDownList3, modify your markup to set autopostback to true and add a reference to that event.

    <asp:DropDownList ID="DropDownList3" runat="server" Width="250px" AutoPostBack="true" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged">
           <asp:ListItem Value="0">Select</asp:ListItem>
           <asp:ListItem Value="approved">Approve</asp:ListItem>
    </asp:DropDownList>

    In your code behind define the DropDownList3_SelectedIndexChanged function:

    protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
            {
                DropDownList dl4 = (DropDownList)FormView1.FindControl("DropDownList4");
                dl4.Enabled = false;
            }

    You also have to decide how you really want to handle this because if someone changes their mind, they can change Approved back to Select, but they can't select Deny.  And I assume you also want to handle the reverse event, selecting Deny. Perhaps you need to check the selected value and disable the other if it is Approve and enable the other it if it is Select.  In any case, wouldn't it make more sense to have one DropDownList with both Approve and Deny as options?  A DropDownList allows only one selection - no need to fuss with enabling and disabling other DropDownsLists to prevent both from being selected.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 6, 2020 11:55 PM
  • User-1559458579 posted

    Excellent @KathyW

    Wednesday, October 7, 2020 9:12 PM