简体   繁体   English

在C#的下拉列表上使用show.hide吗?

[英]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. 我正在使用工具箱控件(例如,文本框,下拉列表)验证用户输入,我希望当用户在下拉列表中选择一个选项时,使其显示或隐藏其下方的Upload控件。

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. 例如,在下面的代码中,当选择“ lone working”作为“ YES Answer”时,我希望下面显示“ Lone Working Procedure”文档的文件上载控件,但是如果答案为“ NO”,则我不希望文件上载控件显示。

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" /> ---%>

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 并使用ViewState来保存您的DropDownList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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