Search code examples
c#asp.netvalidationdrop-down-menuasyncfileupload

Using show.hide on a drop down list in c#?


I am validating user input using toolbox controls eg text boxes, drop down lists, I want to when a user selects a option in a drop down list make it either show or hide a Upload control below it.

For example in the code below when lone working is selected as YES Answer, I wish for the file upload control for a Lone working procedure document to be displayed below, but if it was a NO answer I wouldn't want the File upload control to display.

Any help would be much appreciated.Thanks

  <td class="question">
                             Lone Working:
                    </td>
                    <td>
                        <asp:DropDownList ID="DDLONE" runat="server" Width="150px" 
                            OnSelectedIndexChanged="DDLONE_SelectedIndexChanged">
                            <asp:ListItem Text="Yes"></asp:ListItem>
                            <asp:ListItem Text="No"></asp:ListItem>
                        </asp:DropDownList>
                        <span class="mandatory">*</span>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator27" runat="server" ControlToValidate="DDLONE" ErrorMessage=" Required." InitialValue="Please select one..." ForeColor="Red" SetFocusOnError="true"></asp:RequiredFieldValidator>
                          </td>
                </tr>
                <tr>
                          <td class="question">
                        Lone Working Company Procedure:
                        </td>
                          <td>
                        <asp:AsyncFileUpload ID="AsyncFileUpload3" runat="server" UploaderStyle="Traditional"
                            PersistedStoreType="Session" CssClass="answer" style="float:left" PersistFile="True" CompleteBackColor="#C3D021" />
                        <div class="mandatory" style="display:inline">*</div>
                      <%--- <asp:RequiredFieldValidator ID="AsyncFileUpload3_RequiredFieldValidator" runat="server" 
                            ErrorMessage=" Required."
                            ForeColor="Red"
                            ControlToValidate="AsyncFileUpload3"></asp:RequiredFieldValidator>
                        <asp:TextBox ID="txtUplLone" runat="server" style="display:none" MaxLength="0" /> ---%>

Solution

  • You can try with this code

    1 Add AutopostBack="true"
    <asp:DropDownList ID="DDLONE" runat="server" Width="150px" 
                                OnSelectedIndexChanged="DDLONE_SelectedIndexChanged" AutopostBack="true">
    
    
    2
    
     protected void DDLONE_SelectedIndexChanged(object sender, EventArgs e)
     {
        if(DDLONE.SelectedItem.Text == "Yes")//Adjust
        {
           AsyncFileUpload3.Visible = true;
        }
        else
        {
           AsyncFileUpload3.Visible = false;
        }    
     }
    

    Nota : You must bind your dropdownlist with this method (just one time)

    if(! IsPostBack)
    {
      //Bind
    }
    

    And use ViewState to persist your DropDownList