Hi, In this article we are going to bind checkbox in repeater control and after the we will get the selected record list on button click.
tablecheckbox.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="tablecheckbox.aspx.cs" Inherits="FBTest.tablecheckbox" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater runat="server" ID="RptStudentList">
<HeaderTemplate>
<table class="table table-bordered">
<thead>
<tr>
<th>Select</th>
<th>
First Name
</th>
<th>
Last Name
</th>
<th>
City
</th>
<th>
State
</th>
<th>
Mobile No.
</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:CheckBox ID="ItemSelection" runat="server" /><asp:HiddenField ID="HdnChk" runat="server" Value='<%# Eval("StudentID")%>' /></td>
<td>
<%# Eval("FirstName")%>
</td>
<td>
<%# Eval("LastName")%>
</td>
<td>
<%# Eval("City")%>
</td>
<td>
<%# Eval("State")%>
</td>
<td>
<%# Eval("MobileNO")%>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody> </table>
</FooterTemplate>
</asp:Repeater>
</div>
<div>
<asp:Button Text="Get Selected" ID="btnSubmit" runat="server" OnClick="GetSelected_Click" />
</div>
</form>
</body>
</html>
tablecheckbox.aspx.cs
public partial class tablecheckbox : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=Bits-pc;Initial Catalog=ILLINOISDB;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e) {
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("Select * from StudentMaster",con);
da.Fill(dt);
RptStudentList.DataSource = dt;
RptStudentList.DataBind();
}
protected void GetSelected_Click(object sender, EventArgs e)
{
List<int> itemList = GetCheckedItems(this.RptStudentList);
}
private List<int> GetCheckedItems(Repeater RepeaterControl)
{
List<int> itemList = new List<int>();
foreach (RepeaterItem rptrRow in RptStudentList.Items)
{
CheckBox ItemSelection = rptrRow.FindControl("ItemSelection") as CheckBox;
if (ItemSelection != null && ItemSelection.Checked)
{
HiddenField HdnCheck = (HiddenField)rptrRow.FindControl("HdnChk");
if (HdnCheck != null)
{
int ItemId = Convert.ToInt32(HdnCheck.Value);
itemList.Add(ItemId);
}
}
}
return itemList;
}
}
Output :
tablecheckbox.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="tablecheckbox.aspx.cs" Inherits="FBTest.tablecheckbox" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater runat="server" ID="RptStudentList">
<HeaderTemplate>
<table class="table table-bordered">
<thead>
<tr>
<th>Select</th>
<th>
First Name
</th>
<th>
Last Name
</th>
<th>
City
</th>
<th>
State
</th>
<th>
Mobile No.
</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:CheckBox ID="ItemSelection" runat="server" /><asp:HiddenField ID="HdnChk" runat="server" Value='<%# Eval("StudentID")%>' /></td>
<td>
<%# Eval("FirstName")%>
</td>
<td>
<%# Eval("LastName")%>
</td>
<td>
<%# Eval("City")%>
</td>
<td>
<%# Eval("State")%>
</td>
<td>
<%# Eval("MobileNO")%>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody> </table>
</FooterTemplate>
</asp:Repeater>
</div>
<div>
<asp:Button Text="Get Selected" ID="btnSubmit" runat="server" OnClick="GetSelected_Click" />
</div>
</form>
</body>
</html>
tablecheckbox.aspx.cs
public partial class tablecheckbox : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=Bits-pc;Initial Catalog=ILLINOISDB;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e) {
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("Select * from StudentMaster",con);
da.Fill(dt);
RptStudentList.DataSource = dt;
RptStudentList.DataBind();
}
protected void GetSelected_Click(object sender, EventArgs e)
{
List<int> itemList = GetCheckedItems(this.RptStudentList);
}
private List<int> GetCheckedItems(Repeater RepeaterControl)
{
List<int> itemList = new List<int>();
foreach (RepeaterItem rptrRow in RptStudentList.Items)
{
CheckBox ItemSelection = rptrRow.FindControl("ItemSelection") as CheckBox;
if (ItemSelection != null && ItemSelection.Checked)
{
HiddenField HdnCheck = (HiddenField)rptrRow.FindControl("HdnChk");
if (HdnCheck != null)
{
int ItemId = Convert.ToInt32(HdnCheck.Value);
itemList.Add(ItemId);
}
}
}
return itemList;
}
}
Output :
No comments:
Post a Comment