I am facing issue in Gridview Check box checked and server side checkbox is showing checked = false.
Its really strange and I haven’t seen yet before.
I have written following code.
<script type="text/javascript">
function SelectAll() {
if ($('.SelectAll input:checkbox').attr("checked"))
$('.chkTechs input:checkbox').attr("checked", true);
else
$('.chkTechs input:checkbox').attr("checked", false);
}
function SetCheckBoxes(item) {
//$(item).attr("target").checked // this is to find which element clicked
if ($('.chkTechs input:checkbox').filter(":not(:checked)").length > 0) {
$('.SelectAll input:checkbox').attr("checked", false)
}
else {
$('.SelectAll input:checkbox').attr("checked", true)
}
}
</script>
Server side Button Click
foreach (GridViewRow row in gvList.Rows)
{
CheckBox Checked = (CheckBox)row.FindControl("chkSelect");
bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;
}
The state of server controls is maintained in
viewstateand changing the state of control like you are changing the checked status of checkbox with client script (javascript) is not updated inviewstate. So when you access the control on server side you do not get the changes. You have to store the changes in somehidden fieldand use that hidden field on server side to update your controls. It is the way asp.net implements theviewstate.