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

CheckBox Operations using Csharp and ASP.NET

In this article you will see some checkbox operations with c# and asp.net.

Introduction:

CheckBoxList supports some advance features like add/remove items dynamically and change the display layout of CheckBoxes. Let’s now cover the advance operations of CheckBox. This article will cover the following topics:

  1. Toggle CheckBox Selection
  2. Change CheckBox Display Layout
  3. Add CheckBox Items Dynamically
  4. Dynamically Remove CheckBox Items
  5. Enable/Disable CheckBox Through C#

ASP.NET Script:

The following ASP.NET script is having the following control to demonstrate the functionality:

  1. A CheckBoxList, TextBox and a Label
  2. ASP buttons to perform different operations

Listing 1: ASP.NET Code Snippet

<head runat="server">
    <title>MrBool.com Tutorials - Advance CheckBox Tutorial</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div style="font-weight: bold">
            Advance CheckBox Demo</div>
        <table style="background-color: #FFCCFF">
            <tr>
                <td>
                    <asp:CheckBoxList ID="chkListMobile" runat="server" AutoPostBack="True">
                    </asp:CheckBoxList>
                </td>
            </tr>
            <tr>
                <td>
                    <br />
                    <br />
                    Item:<asp:TextBox ID="txtItem" runat="server" Text=""></asp:TextBox>
                    <br />
                    <asp:Label ID="lblchkListMobile" runat="server" Text=""></asp:Label>
                </td>
            </tr>
        </table>
        <br />
        <br />
        <asp:Button ID="btnToggleSelection" runat="server" Text="Toggle Selection" OnClick="btnToggleSelection_Click" />
        <asp:Button ID="btnChangeLayout" runat="server" Text="Change Layout" OnClick="btnChangeLayout_Click" />
        <asp:Button ID="btnAddCheckBoxItem" runat="server" Text="Add CheckBox Item" OnClick="btnAddCheckBoxItem_Click" />
        <br />
        <asp:Button ID="btnDeleteCheckBoxItem" runat="server" Text="Delete CheckBox Item"
            OnClick="btnDeleteCheckBoxItem_Click" />
        <asp:Button ID="btnEnableDisable" runat="server" Text="Enable/Disable" OnClick="btnEnableDisable_Click" />
        </div>
    </form>
</body>	
Original Screen with Some Mobile Data - One CheckBox is selected and one is Disabled

Figure 1: Original Screen with Some Mobile Data - One CheckBox is selected and one is Disabled

C# Code:

Starting with the core operations in C#, you have to include the following namespaces in your code:

using System.Web.UI.WebControls;

Toggle CheckBox Selection:

The toggle functionality changes the Checkbox selection true to false and false to true. In this case all the checkboxes which are selected get unselected and unselected checkboxes gets selected. So to implement this functionality first loop through all the items of CheckBoxList and check if selected perform un-selection and if unselected perform selection. Note that the disabled checkboxes are not considered in this operation.

Toggle Checkbox Selection - Three Checkboxes are selected and one is unselected

Figure 2: Toggle Checkbox Selection - Three Checkboxes are selected and one is unselected

Listing 2: C# Code to Toggle the Checkbox Selection

protected void btnToggleSelection_Click(object sender, EventArgs e)
    {
        for (int chkIndex = 0; chkIndex < chkListMobile.Items.Count; chkIndex++)
        {
            if (chkListMobile.Items[chkIndex].Selected)
            {
                if (chkListMobile.Items[chkIndex].Enabled)
                    chkListMobile.Items[chkIndex].Selected = false;
            }
            else
            {
                if (chkListMobile.Items[chkIndex].Enabled)
                    chkListMobile.Items[chkIndex].Selected = true;
            }
        }

        lblchkListMobile.Text = "Checkbox Selections Toggled.";
    }

Change CheckBox Display Layout:

You can also change display layout of checkboxes from horizontal to vertical and vertical to horizontal dynamically. There is enum called RepeatDirection use this enum to assign appropriate value.

Checkbox Display - Horizontal Display

Figure 3: Checkbox Display - Horizontal Display

Listing 3: Code to Change the Checkbox Repeat Direction

protected void btnChangeLayout_Click(object sender, EventArgs e)
    {
        if (chkListMobile.RepeatDirection == RepeatDirection.Horizontal)
        {
            chkListMobile.RepeatDirection = RepeatDirection.Vertical;
        }
        else
        {
            chkListMobile.RepeatDirection = RepeatDirection.Horizontal;
        }
        lblchkListMobile.Text = "Layout Changed.";
    }
Add CheckBox Items Dynamically:

Instead of hard coding the checkbox items you can add the items dynamically. There is add() method in items class to add the items in CheckBoxList. Pass an item in this method and it will display in UI.

Dynamic Item Add - Google Mobile is Added

Figure 4: Dynamic Item Add - Google Mobile is Added

Listing 4: C# Code to Add Items Dynamically

protected void btnAddCheckBoxItem_Click(object sender, EventArgs e)
    {
        if (txtItem.Text.Trim() != "")
        {
            chkListMobile.Items.Add(txtItem.Text.Trim());
            lblchkListMobile.Text = "Item Added.";
        }
        else
        {
            lblchkListMobile.Text = "Enter a Item.";
        }
    }

Dynamically Remove CheckBox Items:

Similar to adding items you can delete the items dynamically from CheckBoxList. The below code snippet helps you to delete the particular item from list. Use Remove method to remove by item name and use RemoveAt to remove by index.

Listing 5: C# Code to Delete Items Dynamically

protected void btnDeleteCheckBoxItem_Click(object sender, EventArgs e)
    {
        if (txtItem.Text.Trim() != "")
        {
            ListItem li = chkListMobile.Items.FindByText(txtItem.Text.Trim());
            if (li != null)
            {
                chkListMobile.Items.Remove(txtItem.Text.Trim());

                //chkListMobile.Items.RemoveAt(1);
                lblchkListMobile.Text = "Item Deleted.";
            }
            else
            {
                lblchkListMobile.Text = "No Match Found.";
            }
        }
        else
        {
            lblchkListMobile.Text = "Enter a Item.";
        }
    }

Enable/Disable CheckBox through C#:

This functionally will change the all enabled checkboxes to disabled one and vice versa. The full code snippet is given below. The CheckBox item have a property called “Enabled” and you have to make it true and false as per the requirement.

Toggle the enable/disable - Only one item is enabled now

Figure 5: Toggle the enable/disable - Only one item is enabled now

Listing 6: C# Code to Enable/Disable Checkboxes

protected void btnEnableDisable_Click(object sender, EventArgs e)
    {
        for (int chkIndex = 0; chkIndex < chkListMobile.Items.Count; chkIndex++)
        {
            if (chkListMobile.Items[chkIndex].Enabled)
            {
                chkListMobile.Items[chkIndex].Enabled = false;
            }
            else
            {
                chkListMobile.Items[chkIndex].Enabled = true;
            }
        }
        lblchkListMobile.Text = "Item Enabled/Disabled.";
    }

Conclusion:

Now I have covered almost all the features of CheckBox, if you find hard to understand the advance features, Please refer to my previous post on CheckBox.

As Always, You suggestions and comments are welcomed.



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