Anda di halaman 1dari 3

Selecting a row with a checkbox (server-side) Página 1 de 3

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

RadControls for ASP.NET AJAX Documentation


Selecting a row with a checkbox (server-side)
Send Feedback

RadControls for ASP.NET AJAX


You can use a template column to simulate the behavior of the GridClientSelectColumn, but handling the row selection server-side. Handling selection this
way behaves like a grid with client-side selection, except that users cannot select rows by clicking on them.
To simulate the GridClientSelectColumn in a template column:

1. Create an ItemTemplate that contains a check box.


2. In the OnCheckedChanged event of the check box, set the parent row's Selected property to select or de-select the row.
3. If you are allowing multi-row selection, add a check box to the HeaderItemTemplate.
4. In the OnCheckChanged event of the header check box, set the Selected property of every row in the grid and the Checked property of the
corresponding check boxes in the item templates.

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

Anda mungkin juga menyukai