In this article we will introduce with CheckBoxList control in ASP.NET using C#.
CheckBoxList Example in ASP.NET using C#
CheckBoxList control in ASP.NET is used to select or deselect the item. ChechBoxList is just like a RadioButtonList control in HTML. CheckBoxList control contains CheckBoxes in a group. The user is not restrict for to choose only one answer from CkeckBoxList control. In this example we use a CheckBoxList. it has four items. We have true to Enable AutoPostBack because If it is true, the form is automatically posted back to the server when user click any of the checkbox. If you choose any of the item it will display in the Label. The following figure shows how can we edit item in CkeckBoxList control.
CheckBoxList.aspx (Design page):
ChechBoxList.aspx (source code):
<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master" AutoEventWireup="true" CodeFile="CheckBoxList.aspx.cs" Inherits="CheckBoxList" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <div> <h2 style="color:Green">CheckBoxList in ASP.NET 4 , C#</h2> <h3>Which country will you prefer for holidays?</h3> <asp:CheckBoxList ID="ckeckboxlist1" runat="server" Font-Bold="True" ForeColor="#003300" onselectedindexchanged="ckeckboxlist1_SelectedIndexChanged" AutoPostBack="True"> <asp:ListItem>India</asp:ListItem> <asp:ListItem>Singapur</asp:ListItem> <asp:ListItem>Switzerland</asp:ListItem> <asp:ListItem>Australiya</asp:ListItem> </asp:CheckBoxList> <br /> <br /> <asp:Label ID="Message" runat="server" Font-Bold="True"/> </div> </asp:Content>
CheckBoxList.aspx.cs (C# code file):
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class CheckBoxList : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string msg = "You have selected the following countries:<br />"; if (ckeckboxlist1.Items[0].Selected) { msg = msg + ckeckboxlist1.Items[0].Text + "<br />"; } if (ckeckboxlist1.Items[1].Selected) { msg = msg + ckeckboxlist1.Items[1].Text + "<br />"; } if (ckeckboxlist1.Items[2].Selected) { msg = msg + ckeckboxlist1.Items[2].Text + "<br />"; } if (ckeckboxlist1.Items[3].Selected) { msg = msg + ckeckboxlist1.Items[3].Text + "<br />"; } Message.Text = msg; } protected void ckeckboxlist1_SelectedIndexChanged(object sender, EventArgs e) { } }
[ 0 ] Comments