Click here to Skip to main content
15,312,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a gridview with a checkbox column(using template field). As it is required to check all checkboxes in the gridview plus sometimes the rows could get a lot, like say 20 or more, it can be a hassle to keep checking using a mouse.

Then, I was told that spacebar can also check/uncheck, but only to 1 checkbox. The tab key is needed to move focus to the next checkbox. So I am looking to make the spacebar to check/uncheck, then move focus to the next checkbox without using the tab key. Thanks.

What I have tried:

function clickEnter(obj, event) {
           var keyCode;
           if (event.keyCode > 0) {
               keyCode = event.keyCode;
           }
           else if (event.which > 0) {
               keyCode = event.which;
           }
           else {
               keycode = event.charCode;
           }
           if (keyCode == 32) {
               document.getElementById(obj).focus();
               return false;
           }
           else {
               return true;
           }
       }


<---- Code behind ---->

for(int i = 0; i < GridView1.Rows.Count; i++){
CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[4].FindControl("chkItems") as CheckBox;

CheckBox chkNext = (CheckBox)GridView1.Rows[i + 1].Cells[4].FindControl("chkItems") as CheckBox;

chk.Attributes.Add("onkeypress", "return clickEnter('" + chkNext.ClientID + "', event)");
}


I also tried to foreach gridview and the checked event for checkbox, but i did not have any form of success.
Posted
Comments
Gerry Schmitz 31-Jul-21 11:57am
   
Use the Click event on the checkbox to set focus to the next one (or generate a Tab).

Check / uncheck runs before click.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900