Select All CheckBox in the GridView (ajax)

Source: Internet
Author: User

Modified the code according to a netizen.
Server-side method:
Put a gridview control on the page, configure the data source, edit the column, add a template column, edit the template, and put it into a checkbox control. The Code is as follows:

<Body>
<Form id = "form1" runat = "server">
<Asp: ScriptManager ID = "ScriptManager1" runat = "server"/>
<Div>
<Asp: UpdatePanel ID = "UpdatePanel1" runat = "server">
<ContentTemplate>
<Asp: GridView ID = "GridView1" runat = "server">
<Columns>
<Asp: TemplateField>
<HeaderTemplate>
<Asp: CheckBox ID = "chkAll" runat = "server" OnCheckedChanged = "chkAll_CheckedChanged" AutoPostBack = "True"/>
</HeaderTemplate>
<ItemTemplate>
<Asp: CheckBox ID = "chkItem" runat = "server"/>
</ItemTemplate>
</Asp: TemplateField>
</Columns>
</Asp: GridView>
</ContentTemplate>
</Asp: UpdatePanel>
</Div>
</Form>
</Body>

Backend cs code:

Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Data. SqlClient;

Public partial class Default3: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
String conStr = "server = 192.168.0.118; database = Northwind; Uid = sa; pwd = ";
String SQL = "select * from Region ";
SqlConnection con = new SqlConnection (conStr );
SqlDataAdapter sda = new SqlDataAdapter (SQL, con );
DataSet ds = new DataSet ();
Sda. Fill (ds );
This. GridView1.DataSource = ds;
This. GridView1.DataBind ();
Sda. Dispose ();
Con. Close ();
}
}
Protected void chkAll_CheckedChanged (object sender, EventArgs e)
{
For (int I = 0; I <this. GridView1.Rows. Count; I ++)
{
(CheckBox) this. GridView1.Rows [I]. FindControl ("chkItem"). Checked = (CheckBox) this. GridView1.HeaderRow. FindControl ("chkAll"). Checked;
}
}
}

This article is reposted at http://www.cnblogs.com/oec2003/archive/2007/11/09/954798.html. Please modify it later ~
 

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.