需要在按钮单击时隐藏/取消隐藏动态创建的下拉列表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了需要在按钮单击时隐藏/取消隐藏动态创建的下拉列表相关的知识,希望对你有一定的参考价值。

我有一个按钮,当点击时,会创建一个下拉列表。这部分代码正在运行。但是当再次单击相同的按钮时,我想删除新创建的下拉列表。删除下拉部分无效。总体目标是在用户需要时动态创建下拉列表,如果他想在创建后删除它,他应该能够。

<div style="border-style: dotted; font-family: 'Segoe UI'; border-width: 1px; height: 14%; position: relative;
        width: 100%; top: 0px; left: 0px;">
        <table style="width: 52%; position: relative; left: 90px; font-size: small; font-weight: 400;
            top: 10px;">
            <tr>
                <td class="style1">
                    <span class="style5" style='color: red;'>*</span>Date:
                </td>
                <td>
                    <asp:TextBox ID="Date" runat="server" Style="position: relative; font-family: 'Segoe UI'; width: 100%" OnTextChanged="Date_TextChanged" AutoPostBack="True"></asp:TextBox>
                    <asp:CalendarExtender ID="Date_CalendarExtender" runat="server" TargetControlID="Date">
                    </asp:CalendarExtender>
                </td>
                <td colspan="2">
                    &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
                        ErrorMessage="Enter Date" ControlToValidate="Date" 
                        ValidationGroup="Submit_Validators"></asp:RequiredFieldValidator>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style1">
                    Day:</td>
                <td>
                    <asp:TextBox ID="Day" runat="server" Style="position: relative; font-family: 'Segoe UI'; width: 100%"></asp:TextBox>
                </td>
                <td colspan="2">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style1">
                    Weather</td>
                <td>
                    <asp:DropDownList ID="Weather" runat="server"
                    Style="position: relative; width: 100%" Font-Names="Segoe UI" 
                        DataSourceID="WeatherOptions" DataTextField="Weather_Status" 
                        DataValueField="Weather_Status">
                    </asp:DropDownList>                    
                </td>
                <td>
                <asp:PlaceHolder ID="PlaceHolder1" runat="server">
    </asp:PlaceHolder>
                    <asp:Button ID="Add_weather" runat="server" Text="+" OnClick="AddTextBox" 
                         />
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style1">
                    Temperature</td>
                <td>
                    <asp:TextBox ID="Temperature" runat="server" 
                        Style="position: relative; font-family: 'Segoe UI'; width: 100%"></asp:TextBox>
                </td>
                <td colspan="2">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style1">
                    <span class="style5" style='color: red;'>*</span>Job #:
                </td>
                <td>
                    <asp:TextBox ID="JobNumber" runat="server" Style="position: relative; font-family: 'Segoe UI'; width: 100%"></asp:TextBox>
                </td>
                <td colspan="2">
                    &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
                        ErrorMessage="Enter Job Number" ControlToValidate="JobNumber" 
                        ValidationGroup="Submit_Validators"></asp:RequiredFieldValidator>&nbsp;
                </td>
            </tr>
            <tr>
                <td class="style1">
                    Ticket #:</td>
                <td>
                    <asp:TextBox ID="TicketNumber" runat="server" 
                        Style="position: relative; font-family: 'Segoe UI'; width: 100%" 
                        ReadOnly="True"></asp:TextBox>
                </td>
                <td colspan="2">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style1">
                    Customer Name:
                </td>
                <td>
                    <asp:TextBox ID="CustomerName" runat="server" 
                    Style="position: relative; font-family: 'Segoe UI'; width: 100%"></asp:TextBox>
                </td>
                <td colspan="2">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style1">
                    Location/Project Name:
                </td>
                <td>
                    <asp:TextBox ID="Location" runat="server" Style="position: relative; font-family: 'Segoe UI'; width: 100%"></asp:TextBox>
                </td>
                <td colspan="2">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style1">
                    Project Manager:</td>
                <td>
                    <asp:DropDownList ID="Manager" runat="server" DataSourceID="Product_Manager" 
                        DataTextField="Name" DataValueField="Name"                        
                        Style="position: relative; width: 102%" Font-Names="Segoe UI">
                        <asp:ListItem></asp:ListItem>
                    </asp:DropDownList>
                </td>
                <td colspan="2">
                    &nbsp;</td>
            </tr>
            </table>
        <br />
        <table style="width: 52%; position: relative; left: 90px; font-size: small;">
            <tr>
                <td class="style47">
                    Start Time</td>
                <td class="style47">
                    Finish Time</td>
                <td class="style47">
                    Total Hours</td>
            </tr>
            <tr>
                <td class="style42">
                    <asp:DropDownList ID="Start_Time" runat="server" 
                        Style="position: relative; font-family: 'Segoe UI'; width: 100%" 
                        AutoPostBack="True" onselectedindexchanged="Start_Time_SelectedIndexChanged">
                    </asp:DropDownList>
                </td>
                <td class="style42">
                    <asp:DropDownList ID="Finish_Time" runat="server" 
                        Style="position: relative; font-family: 'Segoe UI'; width: 100%" 
                        AutoPostBack="True" onselectedindexchanged="Finish_Time_SelectedIndexChanged">
                    </asp:DropDownList>
                </td>
                <td class="style42">
                    <asp:TextBox ID="Tkt_Total_Hours" runat="server" 
                        Style="position: relative; font-family: 'Segoe UI'; width: 100%" 
                        AutoPostBack="True" ontextchanged="Tkt_Total_Hours_TextChanged"></asp:TextBox>
                </td>
            </tr>
            </table>                
    </div>

代码背后:

protected void AddTextBox(object sender, EventArgs e)
    {
        foreach (Control item in pnlPageRefresh2.Controls.OfType<DropDownList>())
        {
            if (item.ID == "weather2")
            {
                pnlPageRefresh2.Controls.Remove(item);
                break;
            }
        }
        create_cntrl();

    }


    protected void create_cntrl()
    {
        DropDownList weather2 = new DropDownList();
        weather2.DataSourceID = "WeatherOptions";
        weather2.DataTextField ="Weather_Status" ;
        weather2.DataValueField = "Weather_Status";
        pnlPageRefresh2.ContentTemplateContainer.Controls.Add(weather2);
        PlaceHolder1.Controls.Add(weather2);
    }
答案

你可以注意到你只是在你的foreach循环中删除后再次创建下拉列表..

如下所示更新它..如果找到控件然后删除它,如果没有找到,然后交替创建它如下所示

   protected void AddTextBox(object sender, EventArgs e)
   {
      bool found = false;
      foreach (Control item in pnlPageRefresh2.Controls.OfType<DropDownList>())
      {
          if (item.ID == "weather2")
          {
              found = true;
              pnlPageRefresh2.Controls.Remove(item);
              break;
          }
      }
      //check if not found then create it
      if(!found)
      {
          create_cntrl();
      }
   }

以上是关于需要在按钮单击时隐藏/取消隐藏动态创建的下拉列表的主要内容,如果未能解决你的问题,请参考以下文章

c#的combobox自动隐藏下拉列表的问题

EXCEL表格怎么显示隐藏的行或列

CodeIgniter / GroceryCrud:单选按钮/下拉依赖于取消隐藏隐藏字段

jQuery - 选中/未选中复选框上的动态显示和隐藏按钮,反之亦然[关闭]

ComboBox 下拉列表隐藏

为每个具有类名的 div 动态创建按钮,并创建显示/隐藏单击功能