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 ~