Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

A Guide to Work with CheckBox and CheckBoxList using ASP.NET and Csharp

In this article you will see some guides to work with checkbox and checkboxList using asp.net and c#.

Introduction:

ASP.NET CheckBox and CheckBoxList are web server control which provides an option to user to either select or unselect an item. The CheckBoxList items can be dynamically added by adding a data source. This Article will cover the following points:

  1. Check All Items of CheckBox
  2. Uncheck All Items of CheckBox
  3. Get Selected Items of CheckBox
  4. SelectedIndexChanged Event of CheckBox
  5. Check All Items of CheckBoxList
  6. Uncheck All Items of CheckBoxList
  7. Get Selected Items of CheckBoxList
  8. SelectedIndexChanged Event of CheckBoxList

ASP.Net Script:

The ASP.Net code has the following controls to demonstrate the functionality of CheckBox and CheckBoxList.

  1. Three CheckBox and One CheckBoxList
  2. Two Labels and some Buttons

Listing 1: ASP.NET Script

<head runat="server">
    <title>MrBool.com Tutorials - CheckBox and CheckBoxList</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div style="font-weight: bold">
            CheckBox Demo</div>
        <table style="background-color: #FFFF66">
            <tr>
                <td>
                    <asp:CheckBox ID="chkMango" Text="Mango" runat="server" OnCheckedChanged="chkMango_CheckedChanged" AutoPostBack="true" />
                    <asp:CheckBox ID="chkOrange" Text="Orange" runat="server" OnCheckedChanged="chkOrange_CheckedChanged" AutoPostBack="true" />
                    <asp:CheckBox ID="chkBanana" Text="Banana" runat="server" OnCheckedChanged="chkBanana_CheckedChanged" AutoPostBack="true" />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblCheckBox" runat="server" Text=""></asp:Label>
                </td>
            </tr>
        </table>
        <asp:Button ID="btnCheckAll" runat="server" Text="Check All" OnClick="btnCheckAll_Click" />
        <asp:Button ID="btnUncheckAll" runat="server" Text="Uncheck All" OnClick="btnUncheckAll_Click" />
        <asp:Button ID="btnCheckGetSelected" runat="server" Text="Get Selected" OnClick="btnCheckGetSelected_Click" />
        <br />
        <br />
        <br />
        <div style="font-weight: bold">
            CheckBoxList Demo</div>
        <table style="background-color: #99CC33">
            <tr>
                <td>
                    <asp:CheckBoxList ID="chkList" runat="server" AutoPostBack="True" OnSelectedIndexChanged="chkList_SelectedIndexChanged">
                    </asp:CheckBoxList>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblCheckBoxList" runat="server" Text=""></asp:Label>
                </td>
            </tr>
        </table>
        <asp:Button ID="btnCheckListAll" runat="server" Text="Check All" OnClick="btnCheckListAll_Click" />
        <asp:Button ID="btnUnCheckListAll" runat="server" Text="UncheckAll" OnClick="btnUnCheckListAll_Click" />
        <asp:Button ID="btnCheckListGetSelected" runat="server" Text="Get Selected" OnClick="btnCheckListGetSelected_Click" />
    </div>
    </form>
</body>

 
CheckBox Items

Figure 1: CheckBox Items

CheckBoxList Items

Figure 2: CheckBoxList Items

C# Code:

Starting with C# Code you have to add the following namespaces in the code. No other namespace is required.

using System;

Check All Items of CheckBox:

CheckBox is single checkbox while CheckBoxList is group of CheckBoxes. So to select all items you have to assign true for checked property all checkboxes.

CheckBox - All Item Selected

Figure 3: CheckBox - All Item Selected

Listing 2: CheckBox - Select All

protected void btnCheckAll_Click(object sender, EventArgs e)
    {
        chkBanana.Checked = true;
        chkMango.Checked = true;
        chkOrange.Checked = true;
        lblCheckBox.Text = "All Checked.";
    }

Uncheck All Items of CheckBox:

Similar to check all you have to set checked property to false.

Listing 3: Checkbox - Select None

  
protected void btnUncheckAll_Click(object sender, EventArgs e)
    {
        chkBanana.Checked = false;
        chkMango.Checked = false;
        chkOrange.Checked = false;
        lblCheckBox.Text = "All Unchecked.";

    }

Get Selected Items of CheckBox:

Now to get the selected items from checkbox, you have to check the “checked” property of each and every checkbox.

CheckBox - Get Selected Items

Figure 4: CheckBox - Get Selected Items

Listing 4: CheckBox - Get Selected Items

protected void btnCheckGetSelected_Click(object sender, EventArgs e)
    {
        if (chkMango.Checked)
            lblCheckBox.Text = ", Mango";
        if (chkOrange.Checked)
            lblCheckBox.Text += ", Orange";
        if (chkBanana.Checked)
            lblCheckBox.Text += ", Banana";

        lblCheckBox.Text = lblCheckBox.Text.TrimStart(',');
    }

SelectedIndexChanged Event of CheckBox:

The below is “CheckedChanged” event of CheckBox. On selection change it will display the corresponding message.

Listing 5: CheckBox - SelectedIndexChanged Event

protected void chkMango_CheckedChanged(object sender, EventArgs e)
    {
        if (chkMango.Checked)
            lblCheckBox.Text = "Mango is Checked.";
        else
            lblCheckBox.Text = "Mango is UnChecked.";
    }

Check All Items of CheckBoxList:

CheckBoxList is collection of checkboxes. To select all items of this control you have to loop trough all the items and mark the “selected” property to true.

Listing 6: CheckBoxList - Select all items

protected void btnCheckListAll_Click(object sender, EventArgs e)
    {
        for (int chkcount = 0; chkcount < chkList.Items.Count; chkcount++)
        {
            chkList.Items[chkcount].Selected = true;
        }
        lblCheckBoxList.Text = "All Checked.";
    }

Uncheck All Items of CheckBoxList:

The “unselect all” is similar to “select all”, the only difference is that you have two assign false to “selected” property.

CheckBoxList - All Items Unselected

Figure 5: CheckBoxList - All Items Unselected

Listing 7: CheckBoxList - Deselect all items

protected void btnUnCheckListAll_Click(object sender, EventArgs e)
    {
        for (int chkcount = 0; chkcount < chkList.Items.Count; chkcount++)
        {
            chkList.Items[chkcount].Selected = false;
        }

        lblCheckBoxList.Text = "All UnChecked.";
    }

Get Selected Items of CheckBoxList:

Now we have to get the list of selected items in CheckBoxList. Loop through all the items of this control and if item have the “Selected” property true then add in list.

CheckBoxList - All Selected Items

Figure 6: CheckBoxList - All Selected Items

Listing 8: CheckBoxList - Get all selected items

protected void btnCheckListGetSelected_Click(object sender, EventArgs e)
    {
        for (int chkcount = 0; chkcount < chkList.Items.Count; chkcount++)
        {
            if (chkList.Items[chkcount].Selected)
                lblCheckBoxList.Text += ", " + chkList.Items[chkcount].Text;
        }

        lblCheckBoxList.Text = lblCheckBoxList.Text.TrimStart(',');
    }

SelectedIndexChanged Event of CheckBoxList:

The below is “SelectedIndexChanged” event of CheckBoxList. The event occurs whenever there is a selection change of any item.

Listing 9: CheckBoxList - SelectedIndexChanged

protected void chkList_SelectedIndexChanged(object sender, EventArgs e)
    {
        for (int chkcount = 0; chkcount < chkList.Items.Count; chkcount++)
        {
            if (chkList.Items[chkcount].Selected)
                lblCheckBoxList.Text += ", " + chkList.Items[chkcount].Text;
        }

        lblCheckBoxList.Text = lblCheckBoxList.Text.TrimStart(',');

    }

Conclusion:

I have covered the common features of CheckBox and CheckBoxList. Go through the tutorial and source code and try for all the features, I have discussed above. If you get any questions, please comment.

In my next tutorial I will cover some advance features of CheckBox and CheckBoxList.



I am having total 7+ years of experience in developing applications using C#, VB.NET, ASP.NET, Silverlight, WCF, SQL Server and Oracle. I am holding a M.Tech degree in Intelligent systems. For more information, visit my Linkedin p...

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login