ASP.NET技巧:Gridview中全选与反选,源文件如下:
\n
CREATE TABLE [dbo].[tblStudentInfo](
[Pid] [int] IDENTITY(1,1) NOT NULL,
[CNo] [varchar](8) COLLATE Chinese_PRC_CI_AS NOT NULL,
[CName] [varchar](12) COLLATE Chinese_PRC_CI_AS NOT NULL,
[CSex] [varchar](8) COLLATE Chinese_PRC_CI_AS NOT NULL,
[Iage] [int] NOT NULL,
[CClassNo] [varchar](12) COLLATE Chinese_PRC_CI_AS NOT NULL,
[LinkTel] [varchar](25) COLLATE Chinese_PRC_CI_AS NOT NULL,
[Address] [varchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[Cmmt] [varchar](200) COLLATE Chinese_PRC_CI_AS NULL,
)
\n
配置文件
<connectionStrings>
<add name=”StudentInfoConnectionString” connectionString=”Data Source=.\\SQLEXPRESS;Initial Catalog=StudentInfo;User ID=sa;pwd=sa” providerName=”System.Data.SqlClient”/>
</connectionStrings>
\n
Default.aspx
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
\n
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
\n
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<title>http://www.dwww.cn</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False” DataKeyNames=”CNo”
DataSourceID=”SqlDataSource1″ CellPadding=”4″ ForeColor=”#333333″ GridLines=”None”>
<Columns>
<asp:BoundField DataField=”Pid” HeaderText=”Pid” InsertVisible=”False” ReadOnly=”True”
SortExpression=”Pid” />
<asp:BoundField DataField=”CNo” HeaderText=”CNo” ReadOnly=”True” SortExpression=”CNo” />
<asp:BoundField DataField=”CName” HeaderText=”CName” SortExpression=”CName” />
<asp:BoundField DataField=”CSex” HeaderText=”CSex” SortExpression=”CSex” />
<asp:BoundField DataField=”Iage” HeaderText=”Iage” SortExpression=”Iage” />
<asp:BoundField DataField=”CClassNo” HeaderText=”CClassNo” SortExpression=”CClassNo” />
<asp:BoundField DataField=”LinkTel” HeaderText=”LinkTel” SortExpression=”LinkTel” />
<asp:BoundField DataField=”Address” HeaderText=”Address” SortExpression=”Address” />
<asp:BoundField DataField=”Cmmt” HeaderText=”Cmmt” SortExpression=”Cmmt” />
<asp:TemplateField HeaderText=”全选”>
<HeaderTemplate>
<asp:CheckBox ID=”CheckBox2″ runat=”server” AutoPostBack=”True” Text=”全选” OnCheckedChanged=”aa” />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID=”CheckBox1″ runat=”server” AutoPostBack=”True” />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor=”#990000″ Font-Bold=”True” ForeColor=”White” />
<RowStyle BackColor=”#FFFBD6″ ForeColor=”#333333″ />
<PagerStyle BackColor=”#FFCC66″ ForeColor=”#333333″ HorizontalAlign=”Center” />
<SelectedRowStyle BackColor=”#FFCC66″ Font-Bold=”True” ForeColor=”Navy” />
<HeaderStyle BackColor=”#990000″ Font-Bold=”True” ForeColor=”White” />
<AlternatingRowStyle BackColor=”White” />
</asp:GridView>
<asp:SqlDataSource ID=”SqlDataSource1″ runat=”server” ConnectionString=”<% ConnectionStrings:StudentInfoConnectionString %>”
SelectCommand=”SELECT * FROM [tblStudentInfo]“></asp:SqlDataSource>
</div>
</form>
</body>
</html>
\n
Default.cs
\n
using System;
using System.Data;
using System.Configuration;
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;
\n
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void aa(object sender, System.EventArgs ea)
{
CheckBox cb = (CheckBox)sender;
\n
if (cb.Text == “全选”)
{
foreach (GridViewRow gv in this.GridView1.Rows)
{
CheckBox cd = (CheckBox)gv.FindControl(“CheckBox1″);
cd.Checked = cb.Checked;
cb.Text = “反选”;
}
}
else
{
cb.Text = “反选”;
foreach (GridViewRow gv in this.GridView1.Rows)
{
CheckBox cd = (CheckBox)gv.FindControl(“CheckBox1″);
cd.Checked = cb.Checked;
cb.Text = “全选”;
}
\n
}
}
\n
}
\n
来源:cnblogs