Example E1559
Visible to All Users

Grid View for ASP.NET Web Forms - How to use ASPxCheckBox in DataItemTemplate to implement a selection column

This example demonstrates how to emulate the selection behavior in ASPxGridView control.

Implementation Details

DataItemTemplate contains a ASPxCheckBox control. The CheckedChanged event handler calls the SelectRowOnPage method to select or deselect the corresponding row.

ASPx
<dx:GridViewDataTextColumn Caption="#" > <DataItemTemplate> <dxe:ASPxCheckBox ID="cbCheck" runat="server" AutoPostBack="false" OnLoad="cbCheck_Load" /> </DataItemTemplate> ... </dx:GridViewDataTextColumn>
C#
protected void cbCheck_Load(object sender, EventArgs e) { ... cb.ClientSideEvents.CheckedChanged = String.Format("function (s, e) {{ grid.SelectRowOnPage ({0}, s.GetChecked()); }}", container.VisibleIndex); }

HeaderTemplate contains a ASPxCheckBox control to implement select all functionality. For this check box, the CheckedChanged event handler calls the SelectAllRowsOnPage method to select or deselect all grid rows.

ASPx
<dx:GridViewDataTextColumn Caption="#" > ... <HeaderTemplate > <dxe:ASPxCheckBox ID="SelectAllCheckBox" runat="server" ToolTip="Select/Unselect all rows on the page" ClientSideEvents-CheckedChanged="function(s, e) { grid.SelectAllRowsOnPage(s.GetChecked()); grid.PerformCallback(); }" /> </HeaderTemplate> </dx:GridViewDataTextColumn>

Files to Review

Example Code

WebSite/Default.aspx
ASPx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Register Assembly="DevExpress.Web.v13.1, Version=13.1.14.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web" TagPrefix="dxe" %> <%@ Register Assembly="DevExpress.Web.v13.1, Version=13.1.14.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web" TagPrefix="dxwgv" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>How to use ASPxCheckBox in DataItemTemplate to emulate a selection</title> </head> <body> <form id="frm" runat="server"> <div> <dxwgv:ASPxGridView ID="grid" runat="server" ClientInstanceName="grid" AutoGenerateColumns="False" DataSourceID="sds" KeyFieldName="ProductID"> <Columns> <dxwgv:GridViewDataTextColumn Caption="#"> <DataItemTemplate> <dxe:ASPxCheckBox ID="cbCheck" runat="server" AutoPostBack="false" OnLoad="cbCheck_Load" /> </DataItemTemplate> <HeaderTemplate > <dxe:ASPxCheckBox ID="SelectAllCheckBox" runat="server" ToolTip="Select/Unselect all rows on the page" ClientSideEvents-CheckedChanged="function(s, e) { grid.SelectAllRowsOnPage(s.GetChecked()); grid.PerformCallback(); }" /> </HeaderTemplate> <HeaderStyle HorizontalAlign="Center" /> </dxwgv:GridViewDataTextColumn> <dxwgv:GridViewDataTextColumn FieldName="ProductID" /> <dxwgv:GridViewDataTextColumn FieldName="ProductName" /> <dxwgv:GridViewDataTextColumn FieldName="QuantityPerUnit" /> <dxwgv:GridViewDataTextColumn FieldName="UnitPrice" /> <dxwgv:GridViewDataTextColumn FieldName="UnitsInStock" /> <dxwgv:GridViewDataTextColumn FieldName="UnitsOnOrder" /> <dxwgv:GridViewDataTextColumn FieldName="ReorderLevel" /> </Columns> </dxwgv:ASPxGridView> <asp:SqlDataSource ID="sds" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT [ProductID], [ProductName], [QuantityPerUnit], [UnitPrice], [UnitsInStock], [UnitsOnOrder], [ReorderLevel] FROM [Products]"> </asp:SqlDataSource> </div> </form> </body> </html>
WebSite/Default.aspx.cs(vb)
C#
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using DevExpress.Web; public partial class _Default : System.Web.UI.Page { protected void cbCheck_Load(object sender, EventArgs e) { ASPxCheckBox cb = sender as ASPxCheckBox; GridViewDataItemTemplateContainer container = cb.NamingContainer as GridViewDataItemTemplateContainer; cb.ClientInstanceName = String.Format("cbCheck{0}", container.VisibleIndex); cb.Checked = grid.Selection.IsRowSelected(container.VisibleIndex); cb.ClientSideEvents.CheckedChanged = String.Format("function (s, e) {{ grid.SelectRowOnPage ({0}, s.GetChecked()); }}", container.VisibleIndex); } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.