Showing posts with label CheckBoxList. Show all posts
Showing posts with label CheckBoxList. Show all posts

Wednesday, 17 July 2013

CheckBoxList Example in Asp.net


CheckBoxList is one of the Asp.net server control.By using this we can make multiple selection while using in application.Here i will explain a simple example to select multiple items for orders.By using button click event i will get the selected item values in the list
<html>
<head>
<title>CheckBoxList Example in Asp.net</title>
</head>
<body>
<form id="Form1" runat="server">
<asp:CheckBoxList ID="ChkList" runat="server">
      <asp:ListItem>Asp.net</asp:ListItem>
      <asp:ListItem>Csharp</asp:ListItem>
      <asp:ListItem>Mvc</asp:ListItem>
      <asp:ListItem>Jquery</asp:ListItem>
      </asp:CheckBoxList>
<asp:button ID="btnSelectedList" text="Get Selected Items" runat="server" onclick="btnSelectedList_Click"/>
<asp:Label id="lblSelectedItems" runat="server" />
</form>
</body>
</html>
CodeBehind:
using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class CheckBoxList : System.Web.UI.Page
{
String selectedItems = "Items";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSelectedList_Click(object sender, EventArgs e)
{
for (int j = 0; j< ChkList.Items.Count; j++)
{
if (ChkList.Items[j].Selected)
{
selectedItems = selectedItems + ChkList.Items[j].Text;
selectedItems = selectedItems + ",";
}
}
lblSelectedItems.Text = selectedItems;
}
}