Asp.net checkboxlist control example

Asp.net checkboxlist control example:

In this post we describe checkboxlist web server control of asp.net platform. As we know that the checkbox is a square box in web page which gives authority to user that he/she select a particular thing for next processing of asp.net web page. And the checkboxlist is the collection of checkboxes use for the same purpose; the control checkbox is give only one square but the checkboxlist gives more than one.

Checkboxlist control:

As we know that checkboxlist has more than one checkboxes in list with multi selection facility. Means user can choose one, two, and three ….. Or all checkboxes form given checkboxlist.

Checkboxlist control decleration:

In the asp.net web application programming checkboxlist is very important component. The developers use the control many times in different-2 pages for solving to different-2 selection work. For Example many times we see multi selection options on asp.net web page as select city in order of priority.  Or select your Education qualification etc.
Here we have seen how to get asp.net control on web page on development time. The asp.net platform provides many controls you can use these just by the drag from the toolbox and drop on asp.net page.
Simply if you drag checkboxlist from the toolbox and drop on web page then the code is automatic generated on page. This checkboxlist code is :

<asp:CheckBoxList ID="CheckBoxList1" runat="server">
        </asp:CheckBoxList>

 Here checkboxlist1 is the id of dropped checkboxlist control. And the runat server property of checkboxlist refers that this control rut at server side.

Checkboxlist related other post on asp.net tutorial.

Checkboxlist Event in asp.net:


CallingDataMethods, DataBinding, Disposed, CreatingModelDataSource, Init, Load, PreRender, SelectedIndexChanged, DataBound, TextChanged, Unload, PreRender.

How to get the Items in checkbox control:

For the get items in asp.net checkboxlist we have to method first get by code and second get by graphical user interface. In both methods of get items in checkboxlist gives fallowing result:

<asp:CheckBoxList ID="CheckBoxList1" runat="server">
            <asp:ListItem>item1</asp:ListItem>
            <asp:ListItem>item2</asp:ListItem>
            <asp:ListItem>item3</asp:ListItem>
            <asp:ListItem>item4</asp:ListItem>
        </asp:CheckBoxList>

Here in this checkboxlist we consider 4 items within asp.net:Listitem sub tag. This <asp:listitem>  tag is a sub tag of checkboxlist control use to get the list of items in checkboxlist.

How to check how many checkbox checked in given checkboxlist Example by c# code.

foreach (ListItem li1 in CheckBoxList1.Items)
        {
            if (li1.Selected == true)
            {
                Label1.Text += li1.Text + "<br />";
            }
        } 

Here label control is used to show the checked item of checkboxlist on asp.net page. In this code example first create object of listitem component of asp.net checklist. And then get the selected item from checkboxlist and print on label.

Example of Checkboxlist control in Asp.net using C#:

Asp.net web page code of Example of checkboxlist:

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

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
     <h2>
        Code Example of checkboxlist:</h2>
    <p>
        <asp:CheckBoxList ID="CheckBoxList1" runat="server" Height="177px"
            style="font-weight: 700; font-family: Arial, Helvetica, sans-serif; font-size: large">
            <asp:ListItem>item1</asp:ListItem>
            <asp:ListItem>item2</asp:ListItem>
            <asp:ListItem>item3</asp:ListItem>
            <asp:ListItem>item4</asp:ListItem>
        </asp:CheckBoxList>
    </p>
    <p>
        <asp:Label ID="Label1" runat="server" style="font-weight: 700; color: #FF0000"></asp:Label>
    </p>
    <p>
        <br />

        <asp:Button ID="Button1" runat="server" Text="check" onclick="Button1_Click"
            style="font-weight: 700" Width="93px" />
        &nbsp;</p>
    </div>
    </form>
</body>
</html>
checkboxlist_control_example

Example of checkboxlist c# code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Asp_net_checkboxlist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (ListItem li1 in CheckBoxList1.Items)
        {
            if (li1.Selected == true)
            {
                Label1.Text += li1.Text + "<br />";
            }
        } 
    }
}


Other Asp.net related Examples Post:


Comments

Popular posts from this blog