带有确定按钮的下拉列表将触发搜索文本框和搜索按钮在Web浏览器中可见

时间:2011-06-22 03:59:20

标签: c# asp.net button drop-down-menu

这里我有一个DropDownList和Button确定,点击后,它将启用其他搜索文本框和搜索按钮的可见性。此外,这取决于我选择DropDownList项目。比如说,在我的DropDownList中,我有ProductName和ProductCode.I现在选择ProductName这个ddlist旁边的按钮确定。当我点击按钮确定时,标签,textboxName和buttonSearchName等控件将出现在它下面。 我怎么能做到这一点?

  <asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem>ProductName</asp:ListItem>
                <asp:ListItem>ProductCode</asp:ListItem>
                <asp:ListItem>Category</asp:ListItem>
                <asp:ListItem>SellingPrice</asp:ListItem>
                <asp:ListItem>Quantity</asp:ListItem>
                <asp:ListItem>BrandName</asp:ListItem>
                <asp:ListItem>ReOrderQty</asp:ListItem>
                <asp:ListItem>ReOrderLevel</asp:ListItem>
                <asp:ListItem>Ordered</asp:ListItem>
                <asp:ListItem>Allocated</asp:ListItem>
                <asp:ListItem>FreeQty</asp:ListItem>
            </asp:DropDownList>
            <asp:Button ID="btnOK" runat="server" onclick="btnOK_Click" Text="OK" />
            <br />
            ProductName<asp:TextBox ID="txtSearchProductname" runat="server"></asp:TextBox>
            <asp:Button ID="btnSearchProductName" runat="server" Text="search" 
                onclick="btnSearchProductName_Click" />
            <br />

2 个答案:

答案 0 :(得分:0)

有很多方法可以做到这一点,但是因为你刚开始做的最简单的事情就是将控件放在面板中并更改“btnOK_Click”事件中的可见性。

示例:

<asp:Panel id="searchPanel" runat="server" visible="false">
   your controls here....
</asp:Panel>

要使其可见,请在您的活动中使用以下语法。

searchPanel.Visible = True;

答案 1 :(得分:0)

要回答您的问题,一种方法是(在我之前回答)添加更新面板并将可见性设置为False,但是如果您在同一页面中有其他控件,那么您还需要一个ScriptManager(例如FileUpload控件),如果存在ScriptManager,将无法正常工作。

您也可以使用相同的TextBox搜索所有字段,通过实现检测DropDownList中所选值的方法,并根据该值,搜索算法会相应地更改。

所以我只是将您的txtSearchProduct重命名为txtSearch,我添加了一种通用方法来搜索名为btnSearch_Click的所有条件

 <asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem>ProductName</asp:ListItem>
                <asp:ListItem>ProductCode</asp:ListItem>
                <asp:ListItem>Category</asp:ListItem>
                <asp:ListItem>SellingPrice</asp:ListItem>
                <asp:ListItem>Quantity</asp:ListItem>
                <asp:ListItem>BrandName</asp:ListItem>
                <asp:ListItem>ReOrderQty</asp:ListItem>
                <asp:ListItem>ReOrderLevel</asp:ListItem>
                <asp:ListItem>Ordered</asp:ListItem>
                <asp:ListItem>Allocated</asp:ListItem>
                <asp:ListItem>FreeQty</asp:ListItem>
            </asp:DropDownList>
            <br />
            Search: <asp:TextBox ID="txtSearch" runat="server">
                    </asp:TextBox>
            <asp:Button ID="btnSearch" runat="server" Text="search" 
                onclick="btnSearch_Click" />
            <br />

以下是btnSearch_Click看起来像

的示例
protected void btnSearch_Click(object sender, EventArgs e)
{
    string searchText = this.txtSearch.Text;

    switch (this.DropDownList1.SelectedValue.ToString) {
        case "ProductName":
            string sql = "select * from products where ProductName like '%" + searchText + "%'";
        // the rest of your code goes here
            break;

        case "ProductCode":
            string sql = "select * from products where ProductCode like '%" + searchText + "%'";
        // populate some other control with your productcode search here
            break;

    }
}