Tuesday, May 30, 2006

Checking All CheckBoxes in a GridView

Checking All CheckBoxes in a GridView

Here's the part which shows how to implement client-side JavaScript checking/unchecking
protected void Page_Load(object sender, System.EventArgs e)
// DataBind GridView here

'On every page visit we need to build up the CheckBoxIDs array
foreach(GridViewRow row in grid.Rows)
//Get a programmatic reference to the CheckBox control
CheckBox chk = (CheckBox)row.FindControl("chk");
ClientScript.RegisterArrayDeclaration("CheckBoxIDs", string.Concat("'", chk.ClientID, "'"));

<script type="text/javascript">
function ChangeCheckBoxState(id, checkState)
var cb = document.getElementById(id);
if (cb != null)
cb.checked = checkState;

function ChangeAllCheckBoxStates(checkState)
// Toggles through all of the checkboxes defined in the CheckBoxIDs array
// and updates their value to the checkState input parameter
if (CheckBoxIDs != null)
for (var i = 0; i < CheckBoxIDs.length; i++)
ChangeCheckBoxState(CheckBoxIDs[i], checkState);


<input type="button" value="Check All" onclick="ChangeAllCheckBoxStates(true);" />
<input type="button" value="Uncheck All" onclick="ChangeAllCheckBoxStates(false);" />

No comments:


About Me

My photo
Email me: blog@postjobfree.com