Search code examples
asp.netsearchfiltercheckboxlist

How to display results immediately after selecting checkbox


I am using ASP.net GridView. There are check boxes that filters the results after selecting the checkbox and clicking a search button but I want to remove the button to filter automatically after selecting the check boxes.

The contents display in the gridview are from a database table

ASP

<b><label style="font-size:25px">Brand</label></b>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="SBrand" DataValueField="SBrand">

</asp:CheckBoxList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT DISTINCT [SBrand] FROM [Stock]"></asp:SqlDataSource>

C#

protected void gvStock_SelectedIndexChanged(object sender, EventArgs e)
        {
            string id = gvStock.SelectedRow.Cells[0].Text;
            Response.Redirect("Details.aspx?ID=" + id);
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string chkbox = "";
            Label1.Visible = false;
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    if (chkbox == "")
                    {
                        chkbox = "'" + CheckBoxList1.Items[i].Text + "'";
                    }
                    else
                    {
                        chkbox += "," + "'" + CheckBoxList1.Items[i].Text + "'";
                    }
                    Label1.Text = chkbox;

                    string mainconn = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
                    SqlConnection sqlconn = new SqlConnection(mainconn);
                    string sqlquery = "SELECT [pCode],[pID],[bCode], [SBrand], [SDescription], [sCost] , [sPrice] , [SType] , [sSupplierName] , [sSupplierDirect] FROM Stock where SBrand in (" + Label1.Text + ")";
                    SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
                    sqlconn.Open();
                    SqlDataAdapter sda = new SqlDataAdapter(sqlcomm);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    this.gvStock.DataSource = dt;
                    this.gvStock.DataBind();

                }
            }
        }

Solution

  • ASPX

     <asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="SBrand" DataValueField="SBrand" AutoPostBack="True" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">
    

    .CS

     protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string chkbox = "";
            Label1.Visible = false;
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    if (chkbox == "")
                    {
                        chkbox = "'" + CheckBoxList1.Items[i].Text + "'";
                    }
                    else
                    {
                        chkbox += "," + "'" + CheckBoxList1.Items[i].Text + "'";
                    }
                    Label1.Text = chkbox;
    
                    string mainconn = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
                    SqlConnection sqlconn = new SqlConnection(mainconn);
                    string sqlquery = "SELECT [pCode],[pID],[bCode], [SBrand], [SDescription], [sCost] , [sPrice] , [SType] , [sSupplierName] , [sSupplierDirect] FROM Stock where SBrand in (" + Label1.Text + ")";
                    SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
                    sqlconn.Open();
                    SqlDataAdapter sda = new SqlDataAdapter(sqlcomm);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    this.gvStock.DataSource = dt;
                    this.gvStock.DataBind();
    
                }
            }
        }