Search filtering - DropDownLists and TextBoxes
<p>I'm working on some web app and on one of my content pages I require a search function. I use 2 DropDownLists (one for category and one for search parameters) and a TextBox. </p>

<p>Problem is, when I select value from ddlCategory I need a postback (because it filters the data from a database that is displayed i.e. parts, assemblies...). If there is nothing in the TextBox (no text to search for), I have no problem. But when there is some text in the TextBox, it is cleared and again all the data in that category is displayed and I need to enter search parameters again. Do you understand what is the problem? :) How can this be fixed?</p>

<p>Here is the relevant code:</p>

<pre><code>        &lt;asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="true" onselectedindexchanged="ddlCategory_SelectedIndexChanged"&gt;
        &lt;/asp:DropDownList&gt;
        &lt;asp:TextBox ID="tbSrch" runat="server"&gt;&lt;/asp:TextBox&gt;
        &lt;asp:DropDownList ID="ddlSrch" runat="server"&gt;
        &lt;/asp:DropDownList&gt;
        &lt;asp:Button ID="btnSrch" runat="server" Text="Search" onclick="btnSrch_Click" /&gt;

        //here is code behind:
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGridView(); //bind data from SQL DB to GV

        if (Session["ddlCategory"] != null)  //I need a persistent dropdown list
        {                                   
            BindDropDownLists();
            ddlCategory.SelectedValue = (string)Session["ddlCategory"];
        }
        else
        {
            BindDropDownLists();
        }
    }
}

private void BindGridView()
{
      //Get data from database based on Category and search parameters and bind it to the GV
}

private void BindDropDownLists()  //bind DDLs
{
    ddlKategorija.Items.Add("Value1");
    ddlKategorija.Items.Add("Value2");
    ddlKategorija.Items.Add("Value3");
    ddlKategorija.Items.Add("Value4");

    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable table = new DataTable();

    cmd.CommandText = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = 'MyTable'";
    cmd.CommandType = CommandType.Text;
    cmd.Connection = conn;

    da.Fill(table);

    foreach (DataRow row in table.Rows)
    {
        ddlSrch.Items.Add(row[0].ToString());
    }
}

protected void btnSrch_Click(object sender, EventArgs e)
{
    BindGridView();
}

protected void ddlKategorija_SelectedIndexChanged(object sender, EventArgs e)
{
    Session["ddlCategory"] = ddlCategory.SelectedValue;
    Response.Redirect(Request.RawUrl);
}
</code></pre>
