Dynamic generation of a CheckBoxList

In this article, we discuss how to bind CheckBox with Database.

In ASP.NET, we have two controls. One is CheckBox, and the other is CheckBoxList. Here we discuss CheckBoxList Control.

Using this control, we are able to bind our data dynamically.

I am using a table name "SkillsDetails".

Table Name. Skills

Create table Skills(SkillId int primary key,SkillName nvarchar(50))

Note. copy the above statement and paste into the MS-SQL server and run it; the table is created.

  • Open vs2008 and create a new website
  • Open webpage1.aspx
  • Write the following code
<asp:CheckBoxList ID="chkSkills" runat="server">
</asp: CheckBoxList>

Now open webpage1.aspx.cs file and write the following code in the Page Load Event.

LoadSkills();
private void LoadSkills()
{
    try
    {
        DataTable dt = new Pigo.DBUtility.SqlHelper().GetDataTable("SELECT SkillID, ' ' + SkillName as SkillName from SkillDetails", null);     
        if (dt != null)
        {
            chkSkills.DataValueField = "SkillID";
            chkSkills.DataTextField = "SkillName";
            chkSkills.DataSource = dt;
            chkSkills.DataBind();
        }
        dt = null;
    }
    catch (Exception ex)
    {
        // Handle the exception here
    }
}

Note. In the above LoadSkills(), you see Pigo.DBUtility.SqlHelper().GetDataTable(). Pigo.DBUtility is a namespace created by me, and GetDataTable is a method that takes two parameters; one is String, and the other isSqlParameter array; a DataTable is returned. In this method, my SQL query and the other parameter are null. If you are using this, then please write your database connectivity code and use it.

  • chkSkills.DataValueField: In this field, we assign our unique Id; in this example, I assign "SkillsID" as the Primary key.
  • chkSkills.DataTextField: In this field, we assign a Text field; in this example, I assigned "SkillsName".
  • chkSkills.DataSource: It means from where I get data. According to this Example, I assign dt, which is an instance of DataTable Class, and have some data based on my SQL Query.
  • chkSkills.DataBind(): Using this, we tell the compiler to please bind this data for me.
DataTable dt = new Pigo.DBUtility.SqlHelper().GetDataTable("SELECT SkillID,' '+ SkillName as SkillName  from SkillDetails",null);

Note. This is my database code; you place your database connection code; you write here your own database code &hellip. 

Some functions may help you.

When we talk about CheckBox or CheckBoxList, the first thing that comes to mind is that it will be multiple selections.

The function below tells you how to make a string with multiple selected checkboxes.

public string MakeString(CheckBoxList chkSkills)
{
    int[] Skills = new int[chkSkills.Items.Count];
    StringBuilder Skill = new StringBuilder();
    string delimiter = String.Empty;
    for (int i = 0; i < chkSkills.Items.Count; i++)
    {
        if (chkSkills.Items[i].Selected)
        {
            Skills[i] = Convert.ToInt32(chkSkills.Items[i].Value);
            Skill.Append(delimiter + chkSkills.Items[i].Text);
            delimiter = ", ";
        }
    }
    string str = Skill.ToString();
    return str;
}

Note. In this example,(comma) is a separator. You can replace it according to your convince.

Generate a string array from a string with (Comma) as the separator.

Public String[] GetArray(String str)
{
   // Split() is a readyment function it ll split string based on char     // seprator
    String[] Keyword = str.Split(',');
    return Keyword;
}

CheckBoxList is Empty or not.

The following function returns the total number of selected CheckBox items.

private int CheckboxEmpty(CheckBoxList chkSkills)
{
    int selectCount = 0, unselectCount = 0;
    for (int i = 0; i < chkSkills.Items.Count; i++)
    {
        if (chkSkills.Items[i].Selected)
            selectCount++;
        else
            unselectCount++;
    }
    return selectCount;
}


Recommended Free Ebook
Similar Articles