JavaScript implements the full selection of the DataGrid Client CheckBox column, and the reverse

Source: Internet
Author: User
Tags add format return tostring window client
datagrid| Client

Minimalist format: This is a Shang method, but not universal. This is more convenient if the page has only one DataGrid and only a checkbox in the DataGrid. The main idea is to search the entire page of the checkbox, all of them selected or reversed.
Select All
function Allcheck ()
{
for (Var i=0;i<form1.elements.length;i++)
{
var e=form1.elements[i];
if (e.type== ' checkbox ')
E.checked=true;
}

}
Anti-election
function Revcheck ()
{
for (Var i=0;i<form1.elements.length;i++)
{
var e=form1.elements[i];
if (e.type== ' checkbox ')
e.checked=!e.checked;
}
}


Common simple format
Because the checkbox in the DataGrid is generated in the ASP.net page, his ID is to be changed, so we look for the rules of them, we can find the control accurately, so that the selection and selection and select the operation,
Parameter description:
Prefix: prefix; s: Selection box Id;chk: ID of the selection box;


function Getobj (ObjID)
{
return document.getElementById (ObjID);
}

//Select all
function _selectall (prefix,s,chk)
{
 var Oarr = _getcoll (prefix,s,chk);
 for (Var o in Oarr)
 {
  oarr[o].checked = true;
&NBSP}
}
///Reverse
Function _revselect (prefix,s,chk)
{
 var Oarr = _getcoll (prefix,s,chk);
 for (Var o in Oarr)
 {
  oarr[o].checked =!oarr[o].checked;
 }
}

Obtained value
function _getcoll (PREFIX,S,CHK)
{
var i = s;
var Oarr = new Array ();
while (true)
{
var o = getobj (prefix + ' __ctl ' + i + ' _ ' + chk);
if (o!= null)
{
Oarr.push (o);
}
Else
{
Break
}
i++;
}

return Oarr;
}
Check to see if selected
function _checkselect (PREFIX,S,CHK)
{
var Oarr = _getcoll (PREFIX,S,CHK);
for (Var o in Oarr)
{
if (oarr[o].checked)
{
return true;
}
}
return false;

}

Recommended General detail format: http://www.cnblogs.com/ghd258/archive/2005/11/07/270449.html#Post
/* Paging
2 Parameter Description:
3 prefix: prefix; chkall: full selection box; Chksingle: Radio Box ID
4
5 Use Method:
6 if (E.item.itemtype = = Listitemtype.header)
7 {
8 (CheckBox) e.item.cells[1]. FindControl ("Chkall")). Attributes.Add ("onclick", "checkall (' + this.dg.ClientID.ToString () +" ', ' chkall ', ' chksingle '); ");
9}
10*/
11function Checkall (Prefix,chkall,chksingle)
12{
var Indexchkall;
if (prefix.length!= 0)
15 {
Indexchkall = prefix + "__ctl2_" + chkall;
17}
Or else
19 {
Indexchkall = Chkall;
21}
var objchkall = document.getElementById (Indexchkall);
tempobj var;
for (Var i=0;i<document.forms[0].elements.length;i++)
25 {
Tempobj = Document.forms[0].elements[i];
if (Tempobj.type = "checkbox" && tempobj.id!= indexchkall && tempObj.id.indexOf (chksingle)!=- 1)
28 {
tempobj.checked = objchkall.checked;
30}
31}
32}
33/**//* Paging
34 Parameter Description:
Prefix: prefix; chkall: all marquee; Chksingle: Radio Box ID
36
37 Use Method:
if (E.item.itemtype = = ListItemType.AlternatingItem | | e.item.itemtype = = listitemtype.item)
39 {
(CheckBox) e.item.cells[1]. FindControl ("Chksingle")). Attributes.Add ("onclick", "checksingle (' + this.dg.ClientID.ToString () +" ', ' chkall ', ' chksingle '); ");
41}
42*/
43function Checksingle (Prefix,chkall,chksingle)
649
var Indexchkall;
if (prefix.length!= 0)
47 {
Indexchkall = prefix + "__ctl2_" + chkall;
49}
Or else
51 {
Indexchkall = Chkall;
53}
The var objchkall = document.getElementById (Indexchkall);
var tempobj;
var allcount = 0;
var checkcount = 0;
i=0;i<document.forms[0].elements.length;i++ for (VAR)
59 {
Tempobj = Document.forms[0].elements[i];
if (Tempobj.type = "checkbox" && tempobj.id!= indexchkall && tempObj.id.indexOf (chksingle)!=- 1)
62 {
if (tempobj.checked)
64 {
checkcount++;
66}
Or else
68 {
objchkall.checked = false;
//break;
71}
allcount++;
73}
74}
if (Checkcount!= allcount)
76 {
objchkall.checked = false;
78}
Or else
80 {
Bayi if (allcount!= 0)
82 {
Objchkall.checked = true;
84}
85}
86}
87/**//*
88 Parameter Description:
Prefix: prefix; chkall: all marquee; Chksingle: Radio Box ID
Type:1 "Select All", 2 "anti-election", 3 "Cancel"
91
92 Use Method:
THIS.BTNSELECTALL.ATTRIBUTES.ADD ("OnClick", "Checktype") (' + this.dg.ClientID.ToString () + "', ' chkall ', ' Chksingle ') , 1); ");
THIS.BTNUNSELECTALL.ATTRIBUTES.ADD ("OnClick", "Checktype" (' + this.dg.ClientID.ToString () + "', ' chkall ', ') Chksingle ', 2; ");
THIS.BTNCANCELSELECT.ATTRIBUTES.ADD ("OnClick", "Checktype (") + this.dg.ClientID.ToString () + "', ' chkall ', ' Chksingle ', 3; ");
96*/
97function Checktype (Prefix,chkall,chksingle,type)
98{
The Var Indexchkall;
if (prefix.length!= 0)
101 {
102 Indexchkall = prefix + "__ctl2_" + chkall;
103}
Or else
105 {
Indexchkall = Chkall;
107}
108 var objchkall = document.getElementById (Indexchkall);
109 Var tempobj;
The var allcount = 0;
The var checkcount = 0;
112 for (Var i=0;i<document.forms[0].elements.length;i++)
113 {
114 tempobj = Document.forms[0].elements[i];
Tempobj.type = "checkbox" && tempobj.id!= indexchkall && tempObj.id.indexOf (chksingle)!= -1)
116 {
117 switch (type)
118 {
119 Case 1:
Tempobj.checked = true;
121 break;
122 Case 2:
123 tempobj.checked =!tempobj.checked;
124 break;
Case 3:
126 tempobj.checked = false;
127 break;
128}
129 if (tempobj.checked)
130 {
131 checkcount++;
132}
The allcount++;
134}
135}
136 if (Checkcount!= allcount)
137 {
138 objchkall.checked = false;
139}
140 Else
141 {
if (allcount!= 0)
143 {
144 objchkall.checked = true;
145}
146}
147 Window.event.returnValue = false;
148 return false;
149}
150
151/**//*
152 Parameter Description:
153 prefix: prefix; chkall: all marquee; Chksingle: Radio Box ID
154
155 Use Method:
156 This.btnDelete.Attributes.Add ("OnClick", "Submitcheckbox" "+ this.dg.ClientID.ToString () +", ' chkall ', ' Chksingle ');
157*/
158function Submitcheckbox (prefix,chkall,chksingle,msg)
159{
160 Var Indexchkall;
161 if (prefix.length!= 0)
162 {
163 Indexchkall = prefix + "__ctl2_" + chkall;
164}
165 Else
166 {
167 Indexchkall = Chkall;
168}
169 var Objchkall = document.getElementById (Indexchkall);
170
171 Var Tempobj;
172 var allcount = 0;
173 var checkcount = 0;
174 for (Var i=0;i<document.forms[0].elements.length;i++)
175 {
176 tempobj = Document.forms[0].elements[i];
177 if (Tempobj.type = "checkbox" && tempobj.id!= indexchkall && tempObj.id.indexOf (chksingle)!= -1)
178 {
179 if (tempobj.checked)
180 {
181 checkcount++;
182}
183 allcount++;
184}
185}
186 if (allcount = 0)//No data
187 {
188 Window.alert ("not currently" + MSG + "available for deletion");
189 Window.event.returnValue = false;
190 return false;
191}
The other
193 {
194 if (checkcount = 0)
195 {
196 Window.alert ("+ msg +" not selected for deletion);
197 Window.event.returnValue = false;
198 return false;
199}
Or else
201 {
//if (Window.confirm) ("Are you sure you want to delete the currently selected" + checkcount.tostring () + "" Item?) ") = = False)
203 if (window.confirm) are you sure you want to delete the currently selected + MSG +? ") = = False)
204 {
205 Window.event.returnValue = false;
206 return false;
207}
208}
209}
210}


Http://www.cnblogs.com/skylaugh/archive/2006/12/18/596098.html



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.