Click here to Skip to main content
16,016,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my code page

C#
public partial class BindingCheckboxlist : System.Web.UI.Page
{
    bis_ obj = new bis_();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataSet ds = new DataSet();
            ds = obj.get_Title();
            if (ds.Tables[0].Rows.Count > 0)
            {
                chklist.DataSource = ds.Tables[0];
                chklist.DataTextField = "Title";
                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;
        }
        //string Id1 = "";

        //chklist2.DataSource = chklist.SelectedValue;
        //chklist.DataTextField = chklist.selel;
        //chklist2.DataValueField = chklist.SelectedValue;


        //chklist2.DataBind();
      
        for(int i=0;i<chklist.items.count;i++)>
        {
            if (chklist.Items[i].Selected)
            {
            }
            
            

        }

    }
}



This is designing page:

ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BindingCheckboxlist.aspx.cs" Inherits="BindingCheckboxlist" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table align="center">
    <tr><td>
    
    <asp:CheckBoxList runat="server" ID="chklist"><br />
    </td>
    </tr>
    <tr>
    <td>
    <asp:Button runat="server" ID="btnSubmit" Text="Submit" onclick="btnSubmit_Click" />
    <br />
    </td>
    </tr>
    <tr>
    <td>
    <asp:Label runat="server" ID="lblId"><br />
    
    <asp:Label runat="server" ID="lblName">
    </td>
    </tr>
    <tr>
    </tr>
    <td>
    <asp:CheckBoxList runat="server" ID="chklist2">
    
    </td>
    </table>
    </div>
    </form>
</body>
</html>



here my requirement is if i selectd from first check box list values then bind those same values to second checkbox;
Posted
Updated 1-May-13 0:59am
v2
Comments
Thanks7872 1-May-13 6:45am    
Remove unnecessary code and post only the snippet you need help with.

1 solution

Hello,
Please try this,

ASP.NET
<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>


C#
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>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900