When creating a project, you often need to use the gridview function to select all. You need to add a checkbox control to control the function. Usually after writing it, because the data volume is small (not paging) the test is normal, but the following error occurs after the gridview page is displayed. How can I write code control? Next, let's take a look.
Error Message
The solution is as follows:
On the previous page, the gridview has a checkbox, as shown below:
Add the following code to the page Javascript script: 1 <SCRIPT type = "text/JavaScript">
2 // select all items
3 function checkall (ocheckbox ){
4 var gridview1 = Document. getelementbyid ("<% = gventerprise. clientid %> ");
5 For (I = 1; I <gridview1.rows. length; I ++ ){
6 OBJ = gridview1.rows [I]. cells [0]. getelementsbytagname ("input") [0];
7 if (OBJ! = NULL ){
8 obj. Checked = ocheckbox. checked;
9}
10}
11}
12 </SCRIPT>
Add the following code to the page gridview control: 1 <asp: gridview id = "gventerprise" runat = "server" autogeneratecolumns = "false" allowpaging = "true">
2 <columns>
3 <asp: templatefield>
4 <itemtemplate>
5 <asp: checkbox id = "input" runat = "server"/>
6 <asp: Label id = "lblid" runat = "server" style = "display: none;" text = '<% # BIND ("ID ") %> '> </ASP: Label>
7 </itemtemplate>
8 9 <asp: checkbox id = "checkbox2" type = "checkbox" onclick = "checkall (this)" runat = "server"/>
10 11 </ASP: templatefield>
12 <asp: boundfield datafield = "classname" headertext = "class"/>
13 <asp: boundfield datafield = "Sno" headertext = "student ID"/>
......
14 </columns>
15 </ASP: gridview>
The background code is no longer demonstrated and its usage is different.