Freelance Jobs

CheckBoxList Control in ASP.NET 4.0

Introduction:-
If we want to select multiple choices then we normally used CheckBoxList control, it is used to create multi-selection checkbox group. All the checkbox are associate with the single CheckBoxList control's ID.

I drag a CheckBoxList from the toolbox window and drop in the design mode of the page. By defualt the ID property of the CheckBoxList is CheckBoxList1. So we use the ID of the CheckBoxList control in order to add the items in the CheckBoxList.

Now i discuss how to add items in the CheckBoxList with the help of coding.

protected void Page_Load(object sender, EventArgs e)
{
CheckBoxList1.Items.Add("ASP.NET");
CheckBoxList1.Items.Add("VB.NET");
CheckBoxList1.Items.Add("C#.NET");
CheckBoxList1.Items.Add("Java");

CheckBoxList1.Items.Add(new ListItem("JavaScript","js"));

ListItem lstobj = new ListItem();
lstobj.Text = "CCNA";
lstobj.Selected = true;
CheckBoxList1.Items.Add(lstobj);
}

Here i used 3 way how to add items in the CheckBoxList Control.

1) 1 way to direct used the Items collection of the CheckBoxList and Add method to insert the items.
2) 2 way in the Add method i passed the constructor of the ListItem Class whose contructors accept two parameters one is for the text data that is display and the second is for the value data.
3) 3 way i make an object of ListItem Class after that i set the Text and Selected properties of the ListItem's object and then add the object in the Add methods of the CheckBoxList Class.

0 Comments:

Post a Comment



ONLINE JOBS