Selecting multiple checkboxes inside a GridView control
Introduction
GridView
is a new data bound control introduced by Microsoft in
Visual Studio .NET 2005. Most of the operations like sorting, paging and
selecting items from the GridView
are already built in and you can
use it through the design view. In this article, I will explain how you can
select single as well as all the checkboxes which are inside a
GridView
control.
Selecting Checkboxes inside the GridView Control
GridView
has a CheckboxField
column which maps the
checkbox to a field in the database. In this article we won't be using that, we
will make a checkbox in a template column. Simply add a
asp:checkbox
control in the item template of the
GridView
control. If you are working with a DataGrid
control and want the same functionality, then please check out my article: Selecting
multiple checkboxes inside a DataGrid control.
The HTML code looks something like this:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="PersonID"
DataSourceID="mySource" Width="366px" CellPadding="4"
ForeColor="#333333" GridLines="None">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="PersonID" HeaderText="PersonID"
InsertVisible="False" ReadOnly="True" SortExpression="PersonID" />
<asp:BoundField DataField="Name" HeaderText="Name"
SortExpression="Name" />
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
<HeaderTemplate>
</HeaderTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
Now in the button click event, write this code:
// StringBuilder object
StringBuilder str = new StringBuilder();
// Select the checkboxes from the GridView control
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
bool isChecked = ((CheckBox) row.FindControl("chkSelect")).Checked;
if (isChecked)
{
// Column 2 is the name column
str.Append(GridView1.Rows[i].Cells[2].Text);
}
}
// prints out the result
Response.Write(str.ToString());
The code above just iterates through the GridView
and selects
the checked checkboxes. Later it appends the selected value to a
StringBuilder
object. In order to use StringBuilder
you will need to add the System.Text
namespace.
Making a CheckAll functionality
To add a check-all functionality in the GridView
, simply add a
HTML CheckBox
to the header template of the checkbox column.
<HeaderTemplate>
<input id="chkAll" onclick="javascript:SelectAllCheckboxes(this);"
runat="server" type="checkbox" />
</HeaderTemplate>
SelectAllCheckboxes
JavaScript method:
<script language="javascript">
function SelectAllCheckboxes(spanChk){
// Added as ASPX uses SPAN for checkbox
var oItem = spanChk.children;
var theBox= (spanChk.type=="checkbox") ?
spanChk : spanChk.children.item[0];
xState=theBox.checked;
elm=theBox.form.elements;
for(i=0;i<elm.length;i++)
if(elm[i].type=="checkbox" &&
elm[i].id!=theBox.id)
{
//elm[i].click();
if(elm[i].checked!=xState)
elm[i].click();
//elm[i].checked=xState;
}
}
</script>
This is it. I hope you like the article, happy coding!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix