DataList Header Checkbox to Check All Items

Pavanandey
Posted by Pavanandey under JavaScript category on | Views : 6535
<asp:DataList runat="server" ID="GV" HeaderStyle-CssClass="datalistHead"
Width="100%"
OnItemDataBound="GV_ItemDataBound">
<HeaderTemplate>
<table style="width: 100%;">
<thead>
<tr align="left">
<th align="left">
<asp:CheckBox ID="cbSelectAll" runat="server" Text="Check All" ForeColor="White" />
</th>
</tr>
</thead>
</table>
</HeaderTemplate>
<ItemTemplate>
<table style="width: 100%; background-color: #efefef;">
<tr>
<td style="width: 10px">
<asp:CheckBox runat="server" AutoPostBack="false" ID="CkTrans" />
</td>
</tr>
</table>
<asp:GridView ID="GVV" runat="server"
AutoGenerateColumns="false" Width="100%" HeaderStyle-CssClass="tableHead">
<Columns>

</Columns>
</asp:GridView>
<br />
</ItemTemplate>
</asp:DataList>

function SelectAll(obj) {
var list = document.getElementById("<%=GV.ClientID%>");
var chklist = list.getElementsByTagName("input");
for (var i = 0; i < chklist.length; i++) {
if (chklist[i].type == "checkbox" && chklist[i] != obj) {
chklist[i].checked = document.getElementById(obj).checked;
}
}
}

protected void GV_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
((CheckBox)e.Item.FindControl("cbSelectAll")).Attributes.Add("onclick", "javascript:SelectAll('" + ((CheckBox)e.Item.FindControl("cbSelectAll")).ClientID + "')");
}
}

Comments or Responses

Login to post response