【问题标题】:How to search and dipaly a panel using drop down list selection?如何使用下拉列表选择搜索和显示面板?
【发布时间】:2013-12-16 05:10:50
【问题描述】:

在我开始之前,我想说一件事,这与数据库无关。

我正在使用 asp.net 开发一个网站。由于一些混乱,这里被卡住了。
在我的一个页面中,有许多可用的面板。每个面板都有一个标签(状态名称)和两个文本框。在页面顶部,搜索栏 (下拉列表) 提供了州名称。

现在的情况是,如果用户从下拉列表中选择一个状态,则应搜索具有相应标签(状态名称)的特定面板。

举例

我的下拉列表中有状态 A、B 和 C名称和两个文本框)、面板 y(状态 B 作为标签名称和两个文本框)和面板 z(状态 C 作为标签名称和两个文本框)在 Default.aspx 页面中。

现在我需要,如果用户从下拉列表中选择状态 A,那么特定面板(具有状态 A 的标签)应该显示所有控件

“与数据库没有连接”

希望不要混淆。任何帮助都会对我更有帮助。
提前致谢。

【问题讨论】:

  • 想告诉我们您尝试了什么?
  • 你想从哪里显示隐藏 C#java-script?

标签: c# html asp.net .net


【解决方案1】:

假设 label1 在 Panel1 中,label2 在 Panel2 中

protected void Dropdownlist1_Changed(object sender, EventArgs, e)
{
    string labelTxt= Dropdownlist1.SelectedValue;
    if(labelTxt == label1.Text)
    {
        Panel1.Visible = true;
        Panel2.Visible = false;
    }
    else if(labelTxt == label2.Text)
    {
        Panel1.Visible = false;
        Panel2.Visible = true;
    }

}

【讨论】:

    【解决方案2】:

    这很麻烦,但是您可以根据下拉菜单的选定索引更改事件显示/隐藏控件。这是一个示例,您需要进行修改以适应您的场景。

    protected void Dropdownlist1_Changed(object sender, EventArgs, e)
    {
        string item = Dropdownlist1.SelectedValue;
        if(item == "State 1")
        {
            Panel1.Visible = false;
            Panel2.Visible = true;
        }
    }
    

    【讨论】:

    • 感谢您的回复,但问题是,在这里我需要使用面板中的标签控件检查下拉列表项。请再看一遍我的例子。如果对我的问题感到困惑,请告诉我。
    【解决方案3】:

    HTML

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:Panel ID="pnlDropDown" runat="server">
            <table style="width:100%">
                <tr>
                    <td style="text-align:right;width:30%">
                         <asp:Label ID="lblState" runat="server" Text="Select State"></asp:Label>
                    </td>
                    <td>
                        <asp:DropDownList ID="ddlState" runat="server" AutoPostBack="true" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlState_SelectedIndexChanged">
                        <asp:ListItem></asp:ListItem>
                        <asp:ListItem Value="pnlA">State A</asp:ListItem>
                        <asp:ListItem Value="pnlB">State B</asp:ListItem>
                        <asp:ListItem Value="pnlC">State C</asp:ListItem>
                    </asp:DropDownList>
                    </td>
                    <td></td>
                </tr>
            </table>
        </asp:Panel>
            <asp:Panel ID="pnlA" runat="server" Visible="false">
                 <table style="width:100%">
                <tr>
                    <td style="text-align:right;width:30%">
                        <asp:Label ID="lblStateA" runat="server" Text="State A"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtboxA" runat ="server"></asp:TextBox>
    
                    </td>
                    <td></td>
                </tr>
            </table>
            </asp:Panel>
             <asp:Panel ID="pnlB" runat="server" Visible="false">
                 <table style="width:100%">
                <tr>
                    <td style="text-align:right;width:30%">
                        <asp:Label ID="lblB" runat="server" Text="State B"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtB" runat ="server"></asp:TextBox>
    
                    </td>
                    <td></td>
                </tr>
            </table>
            </asp:Panel>
             <asp:Panel ID="pnlC" runat="server" Visible="false">
                 <table style="width:100%">
                <tr>
                    <td style="text-align:right;width:30%">
                        <asp:Label ID="lblc" runat="server" Text="State C"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtc" runat ="server"></asp:TextBox>
    
                    </td>
                    <td></td>
                </tr>
            </table>
            </asp:Panel>
        </div>
        </form>
    </body>
    </html>
    

    代码背后

    namespace WebApplication1
    {
        public partial class dropdown : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
             {
    
             for (int i = 1; i < ddlState.Items.Count; i++)
            {
                var control = ddlState.Items[i].Value;
                this.FindControl(control).Visible = false;
            }
    
            string item = ddlState.SelectedValue;
            this.FindControl(item).Visible = true;
        }
    }
    

    }

    希望对你有帮助,

    快乐编码

    【讨论】:

      【解决方案4】:

      这很容易,但你需要更合乎逻辑意味着你的面板和下拉列表之间没有关系。

      <asp:DropDownList runat="server" ID="ddlState" AutoPostBack="True" 
          OnSelectedIndexChanged="ddlState_SelectedIndexChanged"  >
          <asp:ListItem Text="StateA" Value="Panel1" />
          <asp:ListItem Text="StateB" Value="Panel2" />
      </asp:DropDownList>
      

      在 C# 中

      protected void ddlState_SelectedIndexChanged(object sender, EventArgs, e)
      {
          //hide all the panels 
          for(int i=0;i<ddlState.Items.Count;i++)
          {
             var control= ddlState.Items[i].Value;
            if(this.FindControl(control)!=null)
                 this.FindControl(control).Visible=false;
          }
      
          //show the selected dropdown list panel
          string item = Dropdownlist1.SelectedValue;
          if(this.FindControl(item)!=null)
             this.FindControl(item).Visible =true;
      
      }
      

      【讨论】:

      • 你的答案很完美,我有个主意。但这里它说一些错误,“对象引用未设置为对象的实例。”
      • this.FindControl(control).Visible=false;在这条线上@Shekhar
      • 您需要检查空值并确保下拉列表中有控件ID
      • 代码工作正常,没有任何错误,但没有产生预期的结果。如果我单击下拉列表,只需加载页面,但没有任何反应。可能是我错过了一些东西。
      • 你能把html放在你的问题中吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多