Code for the full selection function of the CheckBox in the GridView

Source: Internet
Author: User

First of all, we must first make a data GridView.
After the GridView is ready, we add a TemplateField to the backend)
Add a CheckBox to the HeaderTemplate in the newly added template bit for full selection, and then add a CheckBox to the ItemTemplate.

Copy codeThe Code is as follows:
<Asp: TemplateField>
<HeaderTemplate>
<Asp: CheckBox ID = "CheckAll" runat = "server" onclick = "SelectAllCheckboxes (this);" Text = "select all/cancel" ToolTip = "select all at once, cancel the entire selection once again "/>
</HeaderTemplate>
<ItemTemplate>
<Asp: CheckBox ID = "CheckBox2" runat = "server" Text = "select all"/>
</ItemTemplate>
</Asp: TemplateField>

Add the following Java Script at the bottom of the Script

Copy codeThe Code is as follows:
<Script type = "text/javascript">
Function SelectAllCheckboxes (spanChk)
{
Elm = document. forms [0];

For (I = 0; I <elm. length; I ++)
{
If (elm [I]. type = "checkbox" & elm [I]. id! = SpanChk. id)
{
If (elm. elements [I]. checked! = SpanChk. checked)
Elm. elements [I]. click ();
}
}
}
</Script>

If MasterPage is applied to your webpage, place the above JS file on the </asp: Content> standard homepage, now you have completed the entire selection and canceled the entire selection function, next, we want to modify some of the changes (you will never make a full selection/cancellation function, just to make the user fun ), next, place a Button on the Response surface, and switch the cursor to the program section under the two push points, after the code is switched to the program part, we use the following code to capture which columns (ROW) are checked.

Copy codeThe Code is as follows:
Int I;
For (I = 0; I <this. GridView1.Rows. Count; I ++)
{
If (CheckBox) GridView1.Rows [I]. FindControl ("CheckBox2"). Checked)
{
Response. Write (GridView1.DataKeys [I]. Value. ToString ());
// GridView1.DataKeys [I]. Value. ToString () can capture the Value of DataKeys in this column. I set the Value of pk.
}
}

If your GridVie has not set DataKeyNames = "resource bit", the code above will not list the pkvalue values.

With the above method, you can check which columns have been checked by users for other programs that you want to renew.

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.