Grid Selecting Row With Checkbox Server Side
Grid Selecting Row With Checkbox Server Side
http://www.telerik.com/help/aspnet-ajax/grid-selecting-row-with-checkbox-server-side.ht... 02/05/2011
Selecting a row with a checkbox (server-side) Página 2 de 3
Expand All
Note
The Parent property of the check box refers to the template. Therefore, to access the parent row, you must use the parent of the parent.
Note that in the grid declaration, you do not need to enable client-side selection:
ASPX Copy
<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" PageSize="5" Skin="Hay"
DataSourceID="AccessDataSource1" AllowMultiRowSelection="true">
<MasterTableView>
<Columns>
<telerik:GridTemplateColumn UniqueName="CheckBoxTemplateColumn">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="ToggleRowSelection"
AutoPostBack="True" />
</ItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="headerChkbox" runat="server" OnCheckedChanged="ToggleSelectedState"
AutoPostBack="True" />
</HeaderTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView></telerik:RadGrid><asp:AccessDataSource ID="AccessDataSource1"
runat="server" DataFile="~/App_Data/Nwind.mdb" SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [Country] from Customers"
</asp:AccessDataSource>
In the code-behind, the OnCheckedChanged event handlers (ToggleRowSelection and ToggleSelectedState) handle the selection and de-selection of rows:
C# Copy
protected void ToggleRowSelection(object sender, EventArgs e)
{
((sender as CheckBox).NamingContainer as GridItem).Selected = (sender as CheckBox).Checked;
bool checkHeader = true;
foreach (GridDataItem dataItem in RadGrid1.MasterTableView.Items)
{
if (!(dataItem.FindControl("CheckBox1") as CheckBox).Checked)
{
checkHeader = false;
break;
}
}
GridHeaderItem headerItem = RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0] as GridHeaderItem;
(headerItem.FindControl("headerChkbox") as CheckBox).Checked = checkHeader;
}
http://www.telerik.com/help/aspnet-ajax/grid-selecting-row-with-checkbox-server-side.ht... 02/05/2011
Selecting a row with a checkbox (server-side) Página 3 de 3
http://www.telerik.com/help/aspnet-ajax/grid-selecting-row-with-checkbox-server-side.ht... 02/05/2011