Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a sample I have put together for you.</p> <p><strong>ASPX</strong></p> <pre><code>&lt;head runat="server"&gt; &lt;title&gt;&lt;/title&gt; &lt;script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; var allCheckBoxSelector = '#&lt;%=GridView1.ClientID%&gt; input[id*="chkAll"]:checkbox'; var checkBoxSelector = '#&lt;%=GridView1.ClientID%&gt; input[id*="chkSelected"]:checkbox'; function ToggleCheckUncheckAllOptionAsNeeded() { var totalCheckboxes = $(checkBoxSelector), checkedCheckboxes = totalCheckboxes.filter(":checked"), noCheckboxesAreChecked = (checkedCheckboxes.length === 0), allCheckboxesAreChecked = (totalCheckboxes.length === checkedCheckboxes.length); $(allCheckBoxSelector).attr('checked', allCheckboxesAreChecked); } $(document).ready(function () { $(allCheckBoxSelector).live('click', function () { $(checkBoxSelector).attr('checked', $(this).is(':checked')); ToggleCheckUncheckAllOptionAsNeeded(); }); $(checkBoxSelector).live('click', ToggleCheckUncheckAllOptionAsNeeded); ToggleCheckUncheckAllOptionAsNeeded(); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;div&gt; &lt;asp:GridView ID="GridView1" runat="server"&gt; &lt;Columns&gt; &lt;asp:TemplateField&gt; &lt;HeaderTemplate&gt; &lt;asp:CheckBox ID="chkAll" runat="server" /&gt; &lt;/HeaderTemplate&gt; &lt;ItemTemplate&gt; &lt;asp:CheckBox ID="chkSelected" runat="server" /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; </code></pre> <p><strong>C#</strong></p> <pre><code> protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { List&lt;string&gt; lstObjects = new List&lt;string&gt; { "aaa", "bbb" }; GridView1.DataSource = lstObjects; GridView1.DataBind(); } } </code></pre> <h2>If you are using the latest version of jQuery (1.7)</h2> <p>Use the following:</p> <pre><code>&lt;script type="text/javascript"&gt; var allCheckBoxSelector = '#&lt;%=GridView1.ClientID%&gt; input[id*="chkAll"]:checkbox'; var checkBoxSelector = '#&lt;%=GridView1.ClientID%&gt; input[id*="chkSelected"]:checkbox'; function ToggleCheckUncheckAllOptionAsNeeded() { var totalCheckboxes = $(checkBoxSelector), checkedCheckboxes = totalCheckboxes.filter(":checked"), noCheckboxesAreChecked = (checkedCheckboxes.length === 0), allCheckboxesAreChecked = (totalCheckboxes.length === checkedCheckboxes.length); $(allCheckBoxSelector).attr('checked', allCheckboxesAreChecked); } $(document).ready(function () { $(allCheckBoxSelector).click(function () { $(checkBoxSelector).attr('checked', $(this).is(':checked')); ToggleCheckUncheckAllOptionAsNeeded(); }); $(checkBoxSelector).click(ToggleCheckUncheckAllOptionAsNeeded); ToggleCheckUncheckAllOptionAsNeeded(); }); &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload