【问题标题】:How to show hidden dropdown box after a selection is made in another dropdown box in asp.net?在asp.net的另一个下拉框中进行选择后如何显示隐藏的下拉框?
【发布时间】:2013-09-25 15:56:36
【问题描述】:

我在让它工作时遇到了很多问题,所以我的页面上有 2 个下拉菜单,还有一个我想先隐藏。在第二个下拉列表(任何选择)中进行选择后,我希望第三个下拉列表及其标签变得可见。它们都连接到数据库。我以一种简单的方式重新创建了代码的这一方面以提供视觉效果。 我已经在网上搜索了帮助。我是 .NET 的新手,从未使用过 jquery 或 ajax,如果可能的话,我只希望在 C# 中使用它。如果您建议 jquery,请更详细地解释。在这一点上,CS 页面几乎是空的。任何帮助表示赞赏。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Dropdowns</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>



        <asp:DropDownList ID="ddlManu" runat="server" AutoPostBack="True" 
            DataSourceID="SqlDataSource1" DataTextField="Field1" DataValueField="ID" >
        </asp:DropDownList>    
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
            SelectCommand="SELECT [Version] FROM [ProductVersion]"
            DataSourceMode="DataReader">
            </asp:SqlDataSource>


        <asp:DropDownList ID="ddlProduct" runat="server" 
            DataSourceID="SqlDataSource2" DataTextField="Field1" DataValueField="ID" 
            AutoPostBack="True"  >
        </asp:DropDownList>

        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>" 
            ProviderName="<%$ ConnectionStrings:ConnectionString2.ProviderName %>" 
            SelectCommand="SELECT [ID], [Field1], [Field2],[FKID] FROM [MSProducts] 
            WHERE FKID = @ID" DataSourceMode="DataReader">

            <SelectParameters>
            <asp:ControlParameter ControlID="ddlManu" Name="ID"
            PropertyName="SelectedValue" DefaultValue="" />
            </SelectParameters>
        </asp:SqlDataSource>

        <br />
        <asp:Label ID="Label1" runat="server" Text="Category1:"></asp:Label>
        <asp:DropDownList ID="ddlPop" runat="server" DataSourceID="SqlDataSource1">
        </asp:DropDownList>
        <br />
        <br />
        <br />
                    <br />
                    <br />
                    <br /> 

    </form>
</body>
</html>

【问题讨论】:

    标签: c# javascript html asp.net .net


    【解决方案1】:

    首先,将您的第三个 dd 设置为可见 false:

    <asp:DropDownList ID="ddlPop" runat="server" Visible="false"></asp:DropDownList>
    

    然后,在第二个 dd 的 OnSelectedIndexChanged 方法上,执行:

    protected void ddlProduct_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlPop.Visible = true;
    }
    

    我创建了一个小示例来使用 jquery 做同样的事情。 但请注意,要使此方法生效,您需要为您的控件禁用回发。

    http://jsfiddle.net/Gys5Y/1/

    【讨论】:

    • 谢谢您,先生,您的回答有效,我的项目正朝着正确的方向前进。
    【解决方案2】:

    像这样重写你的代码

    <form id="form1" runat="server">
    <div>
    
    
    
        <asp:DropDownList ID="ddlManu" runat="server" AutoPostBack="True" 
            DataSourceID="SqlDataSource1" DataTextField="Field1" DataValueField="ID" >
        </asp:DropDownList>    
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
            SelectCommand="SELECT [Version] FROM [ProductVersion]"
            DataSourceMode="DataReader">
            </asp:SqlDataSource>
    
    
        <asp:DropDownList ID="ddlProduct" runat="server" 
            DataSourceID="SqlDataSource2" DataTextField="Field1" DataValueField="ID" 
           onselectedindexchanged="ddlProductSelectedIndexChanged" autopostback="true">
        </asp:DropDownList>
    
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>" 
            ProviderName="<%$ ConnectionStrings:ConnectionString2.ProviderName %>" 
            SelectCommand="SELECT [ID], [Field1], [Field2],[FKID] FROM [MSProducts] 
            WHERE FKID = @ID" DataSourceMode="DataReader">
    
            <SelectParameters>
            <asp:ControlParameter ControlID="ddlManu" Name="ID"
            PropertyName="SelectedValue" DefaultValue="" />
            </SelectParameters>
        </asp:SqlDataSource>
    
        <br />
        <asp:Label ID="Label1" runat="server" Text="Category1:" visible="false"></asp:Label>
        <asp:DropDownList ID="ddlPop" runat="server" DataSourceID="SqlDataSource1" visible="false">
        </asp:DropDownList>
        <br />
        <br />
        <br />
                    <br />
                    <br />
                    <br /> 
    
    </form>
    

    然后在你的代码上

    protected void ddlProductSelectedIndexChanged(object sender, EventArgs e){
    ddlPop.Visible = true;
    Label1.Visible=true;}
    

    【讨论】:

      【解决方案3】:

      最好在客户端执行此功能。只需为下拉列表的 onchange 事件分配一个 javascript 函数,然后输入类似 $("#myddl").css("display", "none");进去。 通过这种方式,您将避免向服务器发出无用的请求,并且您的应用会运行得更快。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-23
        • 1970-01-01
        • 2013-11-12
        • 1970-01-01
        • 2013-09-16
        相关资源
        最近更新 更多