Checking a Checkbox (in header template) in Gridview and other Chekcboxes (in item templates) get checked as...
I want when I check the checkbox in the header template, then other checkboxes in the Item Template should get checked as well Unfortunately this is not working as expected. I added script referencing a the jquery version 1.6.2 in the head tag of the HTML then a function that which will be fired after the checked box is clicked. Also on the Checkbox in the Header Template, the onclick function has the jquery function which I wrote in the at the head tag. The Alert is working fine. I did this to help me test that Jquery is working. Notice no AutoPostBack attributes But the Toggling is not working.
//this script is in the head tag
function toggleSelectionUsingHeaderCheckBox(source) {
$("#GridView1 input[name$='cbDelete']").each(function () {
$(this).attr('checked', source.checked);
});
//alert('checked');
//the alert above is working fine
}
the Gridview code from columns
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="toggleSelectionUsingHeaderCheckBox(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblEmployeeId" runat="server"
Text='<%# Bind("EmployeeId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="StaffName" HeaderText="Name" />
<asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
<asp:BoundField DataField="LockStatus" HeaderText="Status" />
</Columns>
javascript c# jquery asp.net
add a comment |
I want when I check the checkbox in the header template, then other checkboxes in the Item Template should get checked as well Unfortunately this is not working as expected. I added script referencing a the jquery version 1.6.2 in the head tag of the HTML then a function that which will be fired after the checked box is clicked. Also on the Checkbox in the Header Template, the onclick function has the jquery function which I wrote in the at the head tag. The Alert is working fine. I did this to help me test that Jquery is working. Notice no AutoPostBack attributes But the Toggling is not working.
//this script is in the head tag
function toggleSelectionUsingHeaderCheckBox(source) {
$("#GridView1 input[name$='cbDelete']").each(function () {
$(this).attr('checked', source.checked);
});
//alert('checked');
//the alert above is working fine
}
the Gridview code from columns
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="toggleSelectionUsingHeaderCheckBox(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblEmployeeId" runat="server"
Text='<%# Bind("EmployeeId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="StaffName" HeaderText="Name" />
<asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
<asp:BoundField DataField="LockStatus" HeaderText="Status" />
</Columns>
javascript c# jquery asp.net
Check out the link below whether it answers your question forums.asp.net/t/…
– muhdamean
Dec 31 '18 at 14:33
You don't need that ASP code to troubleshoot this; you just need a simple HTML page. Get it working that way first, then adapt your solution to ASP.
– Robert Harvey♦
Dec 31 '18 at 17:28
add a comment |
I want when I check the checkbox in the header template, then other checkboxes in the Item Template should get checked as well Unfortunately this is not working as expected. I added script referencing a the jquery version 1.6.2 in the head tag of the HTML then a function that which will be fired after the checked box is clicked. Also on the Checkbox in the Header Template, the onclick function has the jquery function which I wrote in the at the head tag. The Alert is working fine. I did this to help me test that Jquery is working. Notice no AutoPostBack attributes But the Toggling is not working.
//this script is in the head tag
function toggleSelectionUsingHeaderCheckBox(source) {
$("#GridView1 input[name$='cbDelete']").each(function () {
$(this).attr('checked', source.checked);
});
//alert('checked');
//the alert above is working fine
}
the Gridview code from columns
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="toggleSelectionUsingHeaderCheckBox(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblEmployeeId" runat="server"
Text='<%# Bind("EmployeeId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="StaffName" HeaderText="Name" />
<asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
<asp:BoundField DataField="LockStatus" HeaderText="Status" />
</Columns>
javascript c# jquery asp.net
I want when I check the checkbox in the header template, then other checkboxes in the Item Template should get checked as well Unfortunately this is not working as expected. I added script referencing a the jquery version 1.6.2 in the head tag of the HTML then a function that which will be fired after the checked box is clicked. Also on the Checkbox in the Header Template, the onclick function has the jquery function which I wrote in the at the head tag. The Alert is working fine. I did this to help me test that Jquery is working. Notice no AutoPostBack attributes But the Toggling is not working.
//this script is in the head tag
function toggleSelectionUsingHeaderCheckBox(source) {
$("#GridView1 input[name$='cbDelete']").each(function () {
$(this).attr('checked', source.checked);
});
//alert('checked');
//the alert above is working fine
}
the Gridview code from columns
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="toggleSelectionUsingHeaderCheckBox(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblEmployeeId" runat="server"
Text='<%# Bind("EmployeeId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="StaffName" HeaderText="Name" />
<asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
<asp:BoundField DataField="LockStatus" HeaderText="Status" />
</Columns>
javascript c# jquery asp.net
javascript c# jquery asp.net
edited Dec 31 '18 at 17:17
VDWWD
23.6k113651
23.6k113651
asked Dec 31 '18 at 14:05
Hassan SalihuHassan Salihu
83
83
Check out the link below whether it answers your question forums.asp.net/t/…
– muhdamean
Dec 31 '18 at 14:33
You don't need that ASP code to troubleshoot this; you just need a simple HTML page. Get it working that way first, then adapt your solution to ASP.
– Robert Harvey♦
Dec 31 '18 at 17:28
add a comment |
Check out the link below whether it answers your question forums.asp.net/t/…
– muhdamean
Dec 31 '18 at 14:33
You don't need that ASP code to troubleshoot this; you just need a simple HTML page. Get it working that way first, then adapt your solution to ASP.
– Robert Harvey♦
Dec 31 '18 at 17:28
Check out the link below whether it answers your question forums.asp.net/t/…
– muhdamean
Dec 31 '18 at 14:33
Check out the link below whether it answers your question forums.asp.net/t/…
– muhdamean
Dec 31 '18 at 14:33
You don't need that ASP code to troubleshoot this; you just need a simple HTML page. Get it working that way first, then adapt your solution to ASP.
– Robert Harvey♦
Dec 31 '18 at 17:28
You don't need that ASP code to troubleshoot this; you just need a simple HTML page. Get it working that way first, then adapt your solution to ASP.
– Robert Harvey♦
Dec 31 '18 at 17:28
add a comment |
2 Answers
2
active
oldest
votes
You just have to loop through all the rows in the gridview, get the checkbox for each row and mark it as checked as shown below.
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="toggleSelectionUsingHeaderCheckBox(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblEmployeeId" runat="server"
Text='<%# Bind("EmployeeId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="StaffName" HeaderText="Name" />
<asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
<asp:BoundField DataField="LockStatus" HeaderText="Status" />
</Columns>
Then javascript
<script>
function toggleSelectionUsingHeaderCheckBox(chkbx) {
var GridView1 = document.getElementById("<%=GridView1.ClientID %>");
for (i = 1; i < GridView1.rows.length; i++) {
GridView1.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = chkbx.checked;
}
}
</script>
The checkbox in the gridview header calls the javascript function which does the looping and checking.
Hope this helps.
add a comment |
This finishes the job for me
var gridViewId = '#<%= GridView1.ClientID %>';
function checkAll(selectAllCheckbox) {
//get all checkboxes within item rows and select/deselect based on select all checked status
//:checkbox is jquery selector to get all checkboxes
$('td :checkbox', gridViewId).prop("checked", selectAllCheckbox.checked);
}
function unCheckSelectAll(selectCheckbox) {
//if any item is unchecked, uncheck header checkbox as well
if (!selectCheckbox.checked)
$('th :checkbox', gridViewId).prop("checked", false);
}
To count number of items checked, use this before performing an action on your button
$(document).ready(function () {
$('#<%=btnDelete.ClientID%>').click(function () {
var gv = document.getElementById("<%= GridView1.ClientID %>");
var inputList = gv.getElementsByTagName("input");
var numChecked = 0;
for (var i = 0; i < inputList.length; i++) {
if (inputList[i].type == "checkbox" && inputList[i].checked) {
numChecked = numChecked + 1;
}
}
if (numChecked == 0) {
alert('No rows selected');
return false;
}
else {
return confirm(numChecked + ' row(s) will be locked or deleted');
}
});
});
then the html markup
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="checkAll(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbDelete" onclick="unCheckSelectAll(this);" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblEmployeeId" runat="server"
Text='<%# Bind("EmployeeId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="StaffName" HeaderText="Name" />
<asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
<asp:BoundField DataField="LockStatus" HeaderText="Status" />
</Columns>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988347%2fchecking-a-checkbox-in-header-template-in-gridview-and-other-chekcboxes-in-it%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You just have to loop through all the rows in the gridview, get the checkbox for each row and mark it as checked as shown below.
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="toggleSelectionUsingHeaderCheckBox(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblEmployeeId" runat="server"
Text='<%# Bind("EmployeeId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="StaffName" HeaderText="Name" />
<asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
<asp:BoundField DataField="LockStatus" HeaderText="Status" />
</Columns>
Then javascript
<script>
function toggleSelectionUsingHeaderCheckBox(chkbx) {
var GridView1 = document.getElementById("<%=GridView1.ClientID %>");
for (i = 1; i < GridView1.rows.length; i++) {
GridView1.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = chkbx.checked;
}
}
</script>
The checkbox in the gridview header calls the javascript function which does the looping and checking.
Hope this helps.
add a comment |
You just have to loop through all the rows in the gridview, get the checkbox for each row and mark it as checked as shown below.
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="toggleSelectionUsingHeaderCheckBox(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblEmployeeId" runat="server"
Text='<%# Bind("EmployeeId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="StaffName" HeaderText="Name" />
<asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
<asp:BoundField DataField="LockStatus" HeaderText="Status" />
</Columns>
Then javascript
<script>
function toggleSelectionUsingHeaderCheckBox(chkbx) {
var GridView1 = document.getElementById("<%=GridView1.ClientID %>");
for (i = 1; i < GridView1.rows.length; i++) {
GridView1.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = chkbx.checked;
}
}
</script>
The checkbox in the gridview header calls the javascript function which does the looping and checking.
Hope this helps.
add a comment |
You just have to loop through all the rows in the gridview, get the checkbox for each row and mark it as checked as shown below.
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="toggleSelectionUsingHeaderCheckBox(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblEmployeeId" runat="server"
Text='<%# Bind("EmployeeId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="StaffName" HeaderText="Name" />
<asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
<asp:BoundField DataField="LockStatus" HeaderText="Status" />
</Columns>
Then javascript
<script>
function toggleSelectionUsingHeaderCheckBox(chkbx) {
var GridView1 = document.getElementById("<%=GridView1.ClientID %>");
for (i = 1; i < GridView1.rows.length; i++) {
GridView1.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = chkbx.checked;
}
}
</script>
The checkbox in the gridview header calls the javascript function which does the looping and checking.
Hope this helps.
You just have to loop through all the rows in the gridview, get the checkbox for each row and mark it as checked as shown below.
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="toggleSelectionUsingHeaderCheckBox(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblEmployeeId" runat="server"
Text='<%# Bind("EmployeeId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="StaffName" HeaderText="Name" />
<asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
<asp:BoundField DataField="LockStatus" HeaderText="Status" />
</Columns>
Then javascript
<script>
function toggleSelectionUsingHeaderCheckBox(chkbx) {
var GridView1 = document.getElementById("<%=GridView1.ClientID %>");
for (i = 1; i < GridView1.rows.length; i++) {
GridView1.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = chkbx.checked;
}
}
</script>
The checkbox in the gridview header calls the javascript function which does the looping and checking.
Hope this helps.
answered Dec 31 '18 at 22:11
Ahmad TijaniAhmad Tijani
1428
1428
add a comment |
add a comment |
This finishes the job for me
var gridViewId = '#<%= GridView1.ClientID %>';
function checkAll(selectAllCheckbox) {
//get all checkboxes within item rows and select/deselect based on select all checked status
//:checkbox is jquery selector to get all checkboxes
$('td :checkbox', gridViewId).prop("checked", selectAllCheckbox.checked);
}
function unCheckSelectAll(selectCheckbox) {
//if any item is unchecked, uncheck header checkbox as well
if (!selectCheckbox.checked)
$('th :checkbox', gridViewId).prop("checked", false);
}
To count number of items checked, use this before performing an action on your button
$(document).ready(function () {
$('#<%=btnDelete.ClientID%>').click(function () {
var gv = document.getElementById("<%= GridView1.ClientID %>");
var inputList = gv.getElementsByTagName("input");
var numChecked = 0;
for (var i = 0; i < inputList.length; i++) {
if (inputList[i].type == "checkbox" && inputList[i].checked) {
numChecked = numChecked + 1;
}
}
if (numChecked == 0) {
alert('No rows selected');
return false;
}
else {
return confirm(numChecked + ' row(s) will be locked or deleted');
}
});
});
then the html markup
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="checkAll(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbDelete" onclick="unCheckSelectAll(this);" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblEmployeeId" runat="server"
Text='<%# Bind("EmployeeId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="StaffName" HeaderText="Name" />
<asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
<asp:BoundField DataField="LockStatus" HeaderText="Status" />
</Columns>
add a comment |
This finishes the job for me
var gridViewId = '#<%= GridView1.ClientID %>';
function checkAll(selectAllCheckbox) {
//get all checkboxes within item rows and select/deselect based on select all checked status
//:checkbox is jquery selector to get all checkboxes
$('td :checkbox', gridViewId).prop("checked", selectAllCheckbox.checked);
}
function unCheckSelectAll(selectCheckbox) {
//if any item is unchecked, uncheck header checkbox as well
if (!selectCheckbox.checked)
$('th :checkbox', gridViewId).prop("checked", false);
}
To count number of items checked, use this before performing an action on your button
$(document).ready(function () {
$('#<%=btnDelete.ClientID%>').click(function () {
var gv = document.getElementById("<%= GridView1.ClientID %>");
var inputList = gv.getElementsByTagName("input");
var numChecked = 0;
for (var i = 0; i < inputList.length; i++) {
if (inputList[i].type == "checkbox" && inputList[i].checked) {
numChecked = numChecked + 1;
}
}
if (numChecked == 0) {
alert('No rows selected');
return false;
}
else {
return confirm(numChecked + ' row(s) will be locked or deleted');
}
});
});
then the html markup
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="checkAll(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbDelete" onclick="unCheckSelectAll(this);" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblEmployeeId" runat="server"
Text='<%# Bind("EmployeeId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="StaffName" HeaderText="Name" />
<asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
<asp:BoundField DataField="LockStatus" HeaderText="Status" />
</Columns>
add a comment |
This finishes the job for me
var gridViewId = '#<%= GridView1.ClientID %>';
function checkAll(selectAllCheckbox) {
//get all checkboxes within item rows and select/deselect based on select all checked status
//:checkbox is jquery selector to get all checkboxes
$('td :checkbox', gridViewId).prop("checked", selectAllCheckbox.checked);
}
function unCheckSelectAll(selectCheckbox) {
//if any item is unchecked, uncheck header checkbox as well
if (!selectCheckbox.checked)
$('th :checkbox', gridViewId).prop("checked", false);
}
To count number of items checked, use this before performing an action on your button
$(document).ready(function () {
$('#<%=btnDelete.ClientID%>').click(function () {
var gv = document.getElementById("<%= GridView1.ClientID %>");
var inputList = gv.getElementsByTagName("input");
var numChecked = 0;
for (var i = 0; i < inputList.length; i++) {
if (inputList[i].type == "checkbox" && inputList[i].checked) {
numChecked = numChecked + 1;
}
}
if (numChecked == 0) {
alert('No rows selected');
return false;
}
else {
return confirm(numChecked + ' row(s) will be locked or deleted');
}
});
});
then the html markup
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="checkAll(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbDelete" onclick="unCheckSelectAll(this);" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblEmployeeId" runat="server"
Text='<%# Bind("EmployeeId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="StaffName" HeaderText="Name" />
<asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
<asp:BoundField DataField="LockStatus" HeaderText="Status" />
</Columns>
This finishes the job for me
var gridViewId = '#<%= GridView1.ClientID %>';
function checkAll(selectAllCheckbox) {
//get all checkboxes within item rows and select/deselect based on select all checked status
//:checkbox is jquery selector to get all checkboxes
$('td :checkbox', gridViewId).prop("checked", selectAllCheckbox.checked);
}
function unCheckSelectAll(selectCheckbox) {
//if any item is unchecked, uncheck header checkbox as well
if (!selectCheckbox.checked)
$('th :checkbox', gridViewId).prop("checked", false);
}
To count number of items checked, use this before performing an action on your button
$(document).ready(function () {
$('#<%=btnDelete.ClientID%>').click(function () {
var gv = document.getElementById("<%= GridView1.ClientID %>");
var inputList = gv.getElementsByTagName("input");
var numChecked = 0;
for (var i = 0; i < inputList.length; i++) {
if (inputList[i].type == "checkbox" && inputList[i].checked) {
numChecked = numChecked + 1;
}
}
if (numChecked == 0) {
alert('No rows selected');
return false;
}
else {
return confirm(numChecked + ' row(s) will be locked or deleted');
}
});
});
then the html markup
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbDeleteHeader" runat="server" onclick="checkAll(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbDelete" onclick="unCheckSelectAll(this);" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblEmployeeId" runat="server"
Text='<%# Bind("EmployeeId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="StaffName" HeaderText="Name" />
<asp:BoundField DataField="AccessRole" HeaderText="Access Role" />
<asp:BoundField DataField="LockStatus" HeaderText="Status" />
</Columns>
answered Jan 1 at 5:58
Hassan SalihuHassan Salihu
83
83
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988347%2fchecking-a-checkbox-in-header-template-in-gridview-and-other-chekcboxes-in-it%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Check out the link below whether it answers your question forums.asp.net/t/…
– muhdamean
Dec 31 '18 at 14:33
You don't need that ASP code to troubleshoot this; you just need a simple HTML page. Get it working that way first, then adapt your solution to ASP.
– Robert Harvey♦
Dec 31 '18 at 17:28