Introduction
This code will show you how to add checkbox in asp gridview with select all functionality and also how to get selected checkbox gridview rows values. I have added only two columns in gridview but you can use as per your requirement.
Code
.aspx page code
add javascript reference to your page.
add javascript reference to your page.
- <script type="text/javascript" src="jquery.js"></script>
- <script type="text/javascript">
- $(document).ready(function () {
- var headerChk = $(".chkHeader input");
- var itemChk = $(".chkItem input");
- headerChk.click(function () {
- itemChk.each(function () {
- this.checked = headerChk[0].checked;
- })
- });
- itemChk.each(function () {
- $(this).click(function () {
- if (this.checked == false)
- {
- headerChk[0].checked = false;
- }
- })
- });
- });
- </script>
- <asp:GridView ID="gvdashboard" ClientIDMode="Static" runat="server" class="table table-striped"
- AutoGenerateColumns="False" GridLines="None" CellPadding="4" ForeColor="#333333">
- <AlternatingRowStyle BackColor="White" />
- <Columns>
- <asp:TemplateField ItemStyle-Width="10px" HeaderStyle-Width="10px">
- <HeaderTemplate>
- <asp:CheckBox ID="chkSelectAll" CssClass="chkHeader" runat="server" />
- </HeaderTemplate>
- <ItemTemplate>
- <asp:CheckBox ID="chkRow" CssClass="chkItem" runat="server" />
- </ItemTemplate>
- <HeaderStyle Width="10px"></HeaderStyle>
- <ItemStyle Width="10px"></ItemStyle>
- </asp:TemplateField>
- <asp:BoundField DataField="ID" HeaderText="ID" ></asp:BoundField>
- <asp:BoundField DataField="Name" HeaderText="Name"></asp:BoundField>
- </Columns>
- <EditRowStyle BackColor="#2461BF" />
- <FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
- <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
- <PagerStyle CssClass="pagination" BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
- <RowStyle BackColor="#EFF3FB" />
- <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
- <SortedAscendingCellStyle BackColor="#F5F7FB" />
- <SortedAscendingHeaderStyle BackColor="#6D95E1" />
- <SortedDescendingCellStyle BackColor="#E9EBEF" />
- <SortedDescendingHeaderStyle BackColor="#4870BE" />
- </asp:GridView>
- foreach (GridViewRow row in gvdashboard.Rows)
- {
- if (row.RowType == DataControlRowType.DataRow)
- {
- CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
- if (chkRow.Checked)
- {
- // do whatever your logic
- }
- }
- }
No comments:
Post a Comment