Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Friends

I write a method to create dynamic check box and it will work fine.

Now I want to how many check boxes is checked and create drop down list with respect of checked check box. But I am unable to create it.

Please help me.
C#
private void AddCheckboxes()
    {
        try
        {
            String sql = "";
            sql = "Select * from Item_type_mst";
            da.SelectCommand = new SqlCommand(sql, conn);
            conn.Open();
            da.SelectCommand.ExecuteReader();
            conn.Close();
            da.Fill(ds);
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
            for (int intControlIndex = 0; intControlIndex <= dt.Rows.Count-1; intControlIndex++)
            {
                chkList1 = new CheckBox();
                chkList1.Text = dt.Rows[intControlIndex][1].ToString(); 
                chkList1.ID = dt.Rows[intControlIndex][0].ToString();
                chkList1.Font.Name = "Verdana";
                chkList1.Font.Size = 9;
                PnlControl.Controls.Add(chkList1);
                PnlControl.Controls.Add(new LiteralControl(""));
            }
        }
        catch (Exception exp)
        {
            throw new Exception(exp.Message);
        }
}

Now I want to check how many check box are checked and count and create drop down list.
Posted
Updated 25-Nov-11 1:23am
v2
Comments
Dinesh Mani 25-Nov-11 8:02am    
Apart from the solution below the code that you have posted contains a lot of unwnated lines of code and bad coding practices, I've pointed a few below -
1. When using data adapter, don't open the connection. Just provide the connection object. data adapter would take care of opening/closing the connection.
2. Check the status of the connection before attempting to close it.
3. Don't close the connection inside the try block. Do it in Finally block.

Just wanted to help.
Member 10066748 29-Jun-13 9:13am    
how to generate checkbox dynamiocally?



dharmendra kumar singh

Rather than using a panel control to hold your collection of Checkboxes you can use a CheckboxList. It allows you to easily create a list of checkboxes from any datasource and you can easily retrive the selected items.

You can directly bind your data source to the Checkboxlist and control the display orientation using properties.
C#
chkList.DataSource=dt;
chkList.DataBind()

The below code illustrates how to fetch the values -
C#
ArrayList SelectedItems
foreach ListItem item in chkList.Items
{
    if (item.Selected) 
    {
        SelectedItems.Add(item.Text);
    }
}

HTH!

Dinesh
Mark as solution if this worked!
 
Share this answer
 
Hi Tiwari A K,

To get the number of selected checkboxes from checkboxlist and to transfer those items to a dropdownlist, do the ff:

int ctr = 0;
foreach(ListItem chk in chkListItems.Items)
{
   ddlItems.Items.Add(chk.Value);
   ctr++;
}


Hope you get the idea.

Mark this as correct answer if it solves your problem.

Best regards,
Eduard
 
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