// java script toggles the state of checkbox between selected and not selected.
<script type="text/javascript" language="javascript">
function toggleCheckBoxes(e)
{
var isChecked = e.checked;
var grid = document.getElementById('gridOpportunityDetails');
var inputElements = grid.getElementsByTagName("input");
for (i = 0; i <= inputElements.length - 1; i++)
{
if (inputElements[i].type == "checkbox")
inputElements[i].checked = isChecked;
}
}
</script>
Code On Asp.Net Page:
<asp:GridView ID="gridOpportunityDetails" DataSource='<%# GetOpportunityDetails() %>' >
<Columns>
<asp:TemplateField ItemStyle-CssClass="dataGridText" ItemStyle-VerticalAlign="top" HeaderStyle-Width="2%" HeaderStyle-BorderStyle="solid">
<HeaderTemplate> <asp:CheckBox ID="chkSelectAll" Checked ="true" runat="server" Text="" onclick="toggleCheckBoxes(this);" /> </HeaderTemplate>
<ItemTemplate> <asp:CheckBox ID="chkSelect" Checked ="true" runat="server" /> </ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>