Hello,
Please try this,
<asp:checkboxlist runat="server" id="chklist" xmlns:asp="#unknown">
</asp:checkboxlist>
<asp:button runat="server" id="btnSubmit" text="Submit" xmlns:asp="#unknown">
onclick="btnSubmit_Click" />
<asp:label runat="server" id="lblId"></asp:label>
<asp:label runat="server" id="lblName"></asp:label>
<div runat="server" id="NewCheckBox"></div></asp:button>
SqlConnection cn = new SqlConnection(@"");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string query = @"SELECT * FROM Products";
SqlDataAdapter ad = new SqlDataAdapter(query, cn);
DataSet ds = new DataSet();
ad.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
chklist.DataSource = ds.Tables[0];
chklist.DataTextField = "ProductName";
chklist.DataValueField = "Id";
chklist.DataBind();
}
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string Id = "";
int var = 0;
for (int i = 0; i < chklist.Items.Count; i++)
{
if (chklist.Items[i].Selected)
{
if (var == 0)
{
Id += chklist.Items[i].Value;
var = var + 1;
}
else
Id += "," + chklist.Items[i].Value;
}
lblId.Text = "You Are selected:" + Id;
}
string Name = "";
for (int i = 0; i < chklist.Items.Count; i++)
{
if (chklist.Items[i].Selected)
{
Name += chklist.Items[i].Text + ",";
}
lblName.Text = "Your Selected citys are:" + Name;
}
List<int> _intid = Id.TrimEnd(',').Split(',').ToList().ConvertAll<int>(s => Convert.ToInt32(s));
if (_intid.Count() > 0)
{
foreach (var item in _intid)
{
string query = @"SELECT * FROM Category where PId=" + item;
SqlDataAdapter ad = new SqlDataAdapter(query, cn);
DataSet ds = new DataSet();
ad.Fill(ds);
CheckBoxList chklst2 = new CheckBoxList();
chklst2.DataSource = ds.Tables[0];
chklst2.DataTextField = "CategoryName";
chklst2.DataValueField = "PId";
chklst2.DataBind();
NewCheckBox.Controls.Add(chklst2);
}
}
}
</int></int>