In ASP. net2.0, The gridview is added to the checkbox for full selection!

Source: Internet
Author: User

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "gridview_checkboxcolumn.aspx.cs" inherits = "gridsamples_gridview_checkboxcolumn" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
<Script language = "JavaScript" type = "text/JavaScript">
Function selectall (OBJ)
{
VaR thetable = obj. parentelement;
VaR I;
VaR J = obj. parentelement. cellindex;

For (I = 0; I <thetable. Rows. length; I ++)
{
VaR objcheckbox = thetable. Rows [I]. cells [J]. firstchild;
If (objcheckbox. Checked! = NULL) objcheckbox. Checked = obj. checked;
}
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: gridview id = "gridview1" runat = "server" allowpaging = "true" autogeneratecolumns = "false"
Datakeynames = "ID" performanceid = "accessperformance1" allowsorting = "true" ondatabinding = "gridview1_databinding" onrowdatabound = "gridview1_rowdatabound">
<Columns>
<Asp: templatefield>
<Itemtemplate>
<Asp: checkbox id = "checkbox1" runat = "server" Checked = "true" text = '<% # databinder. eval (container. dataitem, "ID") %> '/>
</Itemtemplate>
<Headertemplate>
& Nbsp; <input id = "checkall" type = "checkbox" onclick = "selectall (this);"/> select all
</Headertemplate>
</ASP: templatefield>
<Asp: boundfield datafield = "ID" headertext = "ID" insertvisible = "false" readonly = "true"
Sortexpression = "ID"/>
<Asp: boundfield datafield = "name" headertext = "name" sortexpression = "name"/>
<Asp: boundfield datafield = "sex" headertext = "sex" sortexpression = "sex"/>
<Asp: boundfield datafield = "deptid" headertext = "deptid" sortexpression = "deptid"/>
</Columns>
</ASP: gridview>
& Nbsp;

<Asp: accessdatasource id = "accessdatasource1" runat = "server" datafile = "~ /App_data/test. mdb"
Selectcommand = "select [ID], [name], [sex], [deptid] from [employees]"> </ASP: accessdatasource>
<Asp: button id = "button1" runat = "server" onclick = "button#click" text = "showallselecteditem"/>
<Asp: textbox id = "textbox1" runat = "server" width = "200px" readonly = "true"> </ASP: textbox> </div>
</Form>
</Body>
</Html>

********************************** **
*********************************** *
using system;
using system. data;
using system. configuration;
using system. collections;
using system. web;
using system. web. security;
using system. web. ui;
using system. web. UI. webcontrols;
using system. web. UI. webcontrols. webparts;
using system. web. UI. htmlcontrols;

Public partial class gridsamples_gridview_checkboxcolumn: system. web. UI. page
{< br> ///


// obtain or set the set of the selected items
///
protected arraylist selecteditems
{< br> Get
{< br> return (viewstate ["myselecteditems"]! = NULL )? (Arraylist) viewstate ["myselecteditems"]: NULL;
}< br> set
{< br> viewstate ["myselecteditems"] = value;
}< BR >}

Protected void page_load (Object sender, eventargs E)
{

}

Protected void gridview1_databinding (Object sender, eventargs E)
{
// Call the collectselected method to collect the selected items from the current page before each rebinding.
Collectselected ();
}


Protected void gridview1_rowdatabound (Object sender, gridviewroweventargs E)
{
// The processing here is to display the selected status
If (E. Row. rowindex>-1 & this. selecteditems! = NULL)
{
Datarowview ROW = E. Row. dataitem as datarowview;
Checkbox cb = E. Row. findcontrol ("checkbox1") as checkbox;
If (this. selecteditems. Contains (row ["ID"]. tostring ()))
CB. Checked = true;
Else
CB. Checked = false;
}
}
/// <Summary>
/// Collect the selected items from the current page
/// </Summary>
Protected void collectselected ()
{
Arraylist selecteditems = NULL;
If (this. selecteditems = NULL)
Selecteditems = new arraylist ();
Else
Selecteditems = This. selecteditems;

For (INT I = 0; I <this. gridview1.rows. Count; I ++)
{
String id = This. gridview1.rows [I]. cells [1]. text;
Checkbox cb = This. gridview1.rows [I]. findcontrol ("checkbox1") as checkbox;
If (selecteditems. Contains (ID )&&! CB. Checked)
Selecteditems. Remove (ID );
If (! Selecteditems. Contains (ID) & CB. Checked)
Selecteditems. Add (ID );
}
This. selecteditems = selecteditems;
}

Protected void button#click (Object sender, eventargs E)
{
// Finally, you must not forget to collect the selected information of the current page for the last time before performing operations on the selected items.
Collectselected ();

This. textbox1.text = string. empty;
Foreach (Object TMP in this. selecteditems)
This. textbox1.text + = TMP. tostring () + ",";
}
}

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.