简体   繁体   中英

Search data in textbox based on dropdownlist selected value

I've created a Company contact list in asp.net gridview.I need something like in the pic. If someone has done this before, kindly send me a link or some sample code. 根据下拉列表选择的值搜索数据

aspx code

<asp:Label ID="Label1" runat="server" Text="Search By"></asp:Label>

    <asp:TextBox ID="txtSearchName" runat="server"></asp:TextBox>

   <asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem>Name</asp:ListItem>
        <asp:ListItem>Search By Employee Name</asp:ListItem>
        <asp:ListItem>Search By Company Name</asp:ListItem>            
        <asp:ListItem>Search By Mobile</asp:ListItem>
    </asp:DropDownList>

    <asp:Button ID="btnSearch_Click" runat="server" onclick="btnSearch_Click_Click" 
        Text="Search By" />

c#

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlCon = new   SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        SqlCommand cmd = new SqlCommand("Select * from tablename", sqlCon);
        sqlCon.Open();
        GridView1.DataSource = cmd.ExecuteReader();
        GridView1.DataBind();
    }
}


protected void btnSearch_Click_Click(object sender, EventArgs e)
{

    string Query = string.Empty;
    try
    {
        if (sqlCon.State == ConnectionState.Closed)
        {
            sqlCon.Open();
        }
        if (DropDownList1.SelectedValue.ToString() == "Search By Employee Name")
        {
            Query = "select * from tablename where EmployeeName Like '%" + txtSearchName.Text + "%'";
        }
        else if (DropDownList1.SelectedValue.ToString() == "Search By Company Name")
        {
            Query = "select * from tablename where CompanyName Like '" + txtSearchName.Text + "%'";
        }
        else if (DropDownList1.SelectedValue.ToString() == "Search By Mobile")
        {
            Query = "select * from tablename where Mobile Like '%" + txtSearchName.Text + "'";
        }

        SqlDataAdapter sqlDa = new SqlDataAdapter(Query, sqlCon);
        DataSet Ds = new DataSet();
        sqlDa.Fill(Ds);
        GridView1.DataSource = Ds;
        GridView1.DataBind();
    }
    catch (Exception ex)
    {
        HttpContext.Current.Response.Write("<script>alert('wfrmGrid: 11')</script>" + ex.Message);
    }
    finally
    {
        sqlCon.Close();
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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