Add the checkbox column to the DataGrid using JavaScript (select all or not)

Source: Internet
Author: User

(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.
}

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.