請嘗試使用下面的代碼片段。
.ASPX
<head runat="server">
<title></title>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
<script type="text/javascript">
function SelecteHeaderItemClick(obj) {
alert('Header Item Checked');
var grid = $find("<%= RadGrid1.ClientID %>");
if (grid) {
var MasterTable = grid.get_masterTableView();
var Rows = MasterTable.get_dataItems();
for (var i = 0; i < Rows.length; i++) {
var row = Rows[i];
if (parseInt(row.getDataKeyValue('ID')) != 1)
row.set_selected($(obj).is(':checked'));
}
}
}
function SelecteItemClick() {
alert('Item Checked');
}
</script>
</telerik:RadCodeBlock>
.ASPX.CS
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
dynamic data = new[] {
new { ID = 1, Name ="Name1"},
new { ID = 2, Name = "Name2"},
new { ID = 3, Name = "Name3"},
new { ID = 4, Name = "Name4"},
new { ID = 5, Name = "Name5"}
};
RadGrid1.DataSource = data;
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = e.Item as GridDataItem;
(item["SelectColumn"].Controls[0] as CheckBox).Attributes.Add("onclick", "SelecteItemClick();");
}
if (e.Item is GridHeaderItem)
{
GridHeaderItem item = e.Item as GridHeaderItem;
(item["SelectColumn"].Controls[0] as CheckBox).Attributes.Add("onclick", "SelecteHeaderItemClick(this);");
}
}