(1). Functions
1. Retrieve the checkbox in JavaScript and enable full selection and elimination.
The checkbox written in C # must be sent back to the server for execution,
Javascript can be implemented directly on the client, which is more efficient.
(2). Code
1. Main code snippets in the DataGrid:
<Columns>
<Headertemplate> // header template code
<Asp: checkbox id = "chkheader" runat = "server" autopostback = "false" // set autopostback to false without sending back
Onclick = "javascript: selectall (this);"> </ASP: checkbox>
</Headertemplate>
<Itemtemplate> // item template code
<Asp: checkbox id = "chkitem" runat = "server"> </ASP: checkbox>
</Itemtemplate>
</Columns>
2. Add:
<Script language = "JavaScript">
Function selectall (tempcontrol)
{
// Retrieve all other checkboxes except the header Template
VaR thebox = tempcontrol;
Xstate = thebox. checked;
ELEM = thebox. Form. elements;
For (I = 0; I <ELEM. length; I ++)
If (ELEM [I]. type = "checkbox" & ELEM [I]. ID! = Thebox. ID)
{
If (ELEM [I]. Checked! = Xstate)
ELEM [I]. Click ();
}
}
</SCRIPT>
3. When the user selects the background code to obtain the checkbox value of a column:
For (INT I = 0; I <this. DataGrid. Items. Count; I ++)
{
Bool blnifselect = (checkbox) This. DataGrid. items [I]. findcontrol ("chkitem"). checked;
...; // You can perform various operations based on blnifselect.
}