Skip to content

How use drop down list as as search text

Anil Jain

Asked Apr 7, 2012, 12:55 AM EDT1 answer
Share
LinkedInX
Sir,

How to use drop down list as a search text box like if country name Like U press then come like United states, United Kingdom etc.
how to use drop down in this way

Anil Jain

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Senthilkumar
    0
    Hi,

    Design the gridview control.

    <asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False"
    AllowPaging="True" AllowSorting="true" DataSourceID="dsGridview" Width="540px"
    PageSize="10">
    <Columns>
        <asp:BoundField DataField="id" HeaderText="ID" SortExpression="id" />
        <asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
            <ItemStyle Width="120px" HorizontalAlign="Left" />
            <ItemTemplate>
                <asp:Label ID="lblFirstname" Text='<%#
                    HighlightText(Eval("FirstName")) %>' runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
            <ItemStyle Width="120px" HorizontalAlign="Left" />
            <ItemTemplate>
                <asp:Label ID="lblLastname" Text='<%#
                HighlightText(Eval("LastName")) %>' runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Department" HeaderText="Department"
            SortExpression="Department" ItemStyle-Width="130px" />
        <asp:BoundField DataField="Location" HeaderText="Location"
            SortExpression="Location" ItemStyle-Width="130px" />
    </Columns>
    </asp:GridView>

    Place the drop down list as well.

    Bind the all country names into the drop down list control.
    <asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true"></asp:DropDownList>

    Fill the drop down
    SQL
                string _ConnectionString = "Data Source=SSD438\SQLEXPRESS;Initial Catalog=Master;Integrated Security=true;";
        SqlConnection sqlConnection = new SqlConnection();
                sqlConnection.ConnectionString = _ConnectionString;
                sqlConnection.Open();
                string sqlQuery = "SELECT * FROM Country";
                SqlCommand sqlCommand = new SqlCommand(sqlQuery, sqlConnection);
                sqlCommand.CommandType = CommandType.Text;
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
                DataSet dataSet = new DataSet();
                sqlDataAdapter.Fill(dataSet);
                ddlCountry.DataSource = dataSet.Tables[0];
                ddlCountry.DataTextField = "CountryName";
                ddlCountry.DataValueField = CountryID";
                ddlCountry.DataBind();
    
    
        protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
        SqlConnection sqlConnection = new SqlConnection();
                sqlConnection.ConnectionString = _ConnectionString;
                sqlConnection.Open();
                string sqlQuery = "SELECT * FROM Employees WHERE name LIKE "+ddlCountry.SelectedItem.Value.ToString();
                SqlCommand sqlCommand = new SqlCommand(sqlQuery, sqlConnection);
                sqlCommand.CommandType = CommandType.Text;
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
                DataSet dataSet = new DataSet();
                sqlDataAdapter.Fill(dataSet);
                GridView1.DataSource = dataSet.Tables[0];
                gridView1.DataBind();
        }
    0