Refer:
- Filter ASP.Net GridView data based on CheckBoxList Checked Items
Please consider the above example and based on the CheckBox1 List Selection bind CheckBoxList2.
HTML
<asp:CheckBoxList ID="chkCountries" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Country_Selected">
<asp:ListItem Text="Argentina" Value="Argentina"></asp:ListItem>
<asp:ListItem Text="Austria" Value="Austria"></asp:ListItem>
<asp:ListItem Text="Belgium" Value="Belgium"></asp:ListItem>
<asp:ListItem Text="Brazil" Value="Brazil"></asp:ListItem>
<asp:ListItem Text="Canada" Value="Canada"></asp:ListItem>
</asp:CheckBoxList>
<br />
<hr />
<br />
<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Country_Selected">
</asp:CheckBoxList>
C#
protected void Country_Selected(object sender, EventArgs e)
{
this.BindCheckBoxList();
}
private void BindCheckBoxList()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT * FROM Customers";
string condition = string.Empty;
foreach (ListItem item in chkCountries.Items)
{
condition += item.Selected ? string.Format("'{0}',", item.Value) : "";
}
if (!string.IsNullOrEmpty(condition))
{
condition = string.Format(" where Country in ({0})", condition.Substring(0, condition.Length - 1));
}
SqlCommand cmd = new SqlCommand(query + condition);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
CheckBoxList1.DataTextField = "ContactName";
CheckBoxList1.DataValueField = "CustomerId";
CheckBoxList1.DataSource = ds;
CheckBoxList1.DataBind();
}
}
}
}
Screenshot