Tuesday, September 28, 2010

CheckBoxList Server Control

CheckBoxList Server Control

The CheckBoxList is almost similar to the CheckBox, except user can select more than one option in CheckBoxList. We can work on collection of items rather than using a single item.

CheckBoxList will have a collection of related items. Example shown below,

Like Movies, Cars, Bikes etc.

Adding items in source file.


Image comes here……


Source code….

<asp:Label ID="lblMovies" runat="server" Text="Movies:" Font-Bold="true" />
<asp:CheckBoxList ID="chklMovies" ForeColor="OrangeRed" runat="server">
<asp:ListItem Text="Titanic" Value="1" />
<asp:ListItem Text="Cops Out" Value="2" />
<asp:ListItem Text="Iron Man" Value="3" />
<asp:ListItem Text="3 Idiots" Value="4" />
</asp:CheckBoxList>
<asp:Label ID="lblCars" runat="server" Text="Cars:" Font-Bold="true" />
<asp:CheckBoxList ID="chklCars" ForeColor="OrangeRed" runat="server">
<asp:ListItem Text="Honda" />
<asp:ListItem Text="Acura" />
<asp:ListItem Text="BMW" />
<asp:ListItem Text="Toyota" />
</asp:CheckBoxList>
<asp:Label ID="lblBikes" runat="server" Text="Bikes:" Font-Bold="true" />
<asp:CheckBoxList ID="chklBikes" ForeColor="OrangeRed" runat="server">
<asp:ListItem Text="Hero Honda" />
<asp:ListItem Text="Baja" />
<asp:ListItem Text="TVS" />
<asp:ListItem Text="Honda" />
</asp:CheckBoxList>

Adding items in .cs

Image comes here…..

.aspx code….
<asp:Label ID="lblMovies" runat="server" Text="Movies:" Font-Bold="true" />
<asp:CheckBoxList ID="chklMovies" ForeColor="OrangeRed" runat="server" />
<asp:Label ID="lblCars" runat="server" Text="Cars:" Font-Bold="true" />
<asp:CheckBoxList ID="chklCars" ForeColor="OrangeRed" runat="server" />
<asp:Label ID="lblBikes" runat="server" Text="Bikes:" Font-Bold="true" />
<asp:CheckBoxList ID="chklBikes" ForeColor="OrangeRed" runat="server" />


.cs code….

chklMovies.Items.Add("Titanic");
chklMovies.Items.Add("Cops Out");
chklMovies.Items.Add("Iron Man");
chklMovies.Items.Add("3 Idiots");

chklCars.Items.Add("Honda");
chklCars.Items.Add("Acura");
chklCars.Items.Add("BMW");
chklCars.Items.Add("Toyota");

chklBikes.Items.Add("Hero Honda");
chklBikes.Items.Add("Baja");
chklBikes.Items.Add("TVS");
chklBikes.Items.Add("Honda");


Examples on how to handle events usinf Autopostback property.

.aspx

<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
<asp:Label ID="lblMovies" Text="Select your Favorate movie:<br/>" runat="server"
Font-Bold="true" />
<asp:CheckBoxList AutoPostBack="true" ID="chklMovies" ForeColor="Violet" runat="server"
OnSelectedIndexChanged="chklMovies_SelectedIndexChanged">
<asp:ListItem Text="Titanic" />
<asp:ListItem Text="Cops Out" />
<asp:ListItem Text="Iron Man" />
<asp:ListItem Text="3 Idiots" />
</asp:CheckBoxList>
<asp:Label ID="lbloutput" runat="server" Font-Bold="true" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>

.cs

protected void chklMovies_SelectedIndexChanged(object sender, EventArgs e)
{

Int16 flag = 0;
String str=String.Empty;
Int16 i = 1;
foreach (ListItem item in chklMovies.Items)
{
if (item.Selected == true)
{
str +=i+") "+ item.Text + "<br/>";
flag = 1;
i++;
}
}

if (flag == 0)
{
lbloutput.Text = "Select atleast one movie";
lbloutput.ForeColor = Color.Red;
}
else
{
lbloutput.Text = "You selected " + (i-1) + " Movies<br/>"+str;
lbloutput.ForeColor = Color.Black;
}
}

Copy, paste and execute. Let me know if any questions through commenting on this article

No comments:

Post a Comment