GridView和CheckBox结合[转]
效果图:
后台代码:
Code
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Data.SqlClient;
11
12public partial class Default5 : System.Web.UI.Page
13{
14 SqlConnection sqlcon;
15 string strCon = "Data Source=(local);Database=db1;Uid=sa;Pwd=sa";
16
17 protected void Page_Load(object sender, EventArgs e)
18 {
19 if (!IsPostBack)
20 {
21 bind();
22 }
23 }
24
25 protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
26 {
27 for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
28 {
29 CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
30 if (CheckBox2.Checked == true)
31 {
32 cbox.Checked = true;
33 }
34 else
35 {
36 cbox.Checked = false;
37 }
38 }
39 }
40
41 protected void Button2_Click(object sender, EventArgs e)
42 {
43 sqlcon = new SqlConnection(strCon);
44 SqlCommand sqlcom;
45 for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
46 {
47 CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
48 if (cbox.Checked == true)
49 {
50
51 string sqlstr = "delete from table1 where 身份证号码='" + GridView1.DataKeys[i].Value + "'";
52 sqlcom = new SqlCommand(sqlstr, sqlcon);
53 sqlcon.Open();
54 sqlcom.ExecuteNonQuery();
55 sqlcon.Close();
56 }
57 }
58 bind();
59 }
60
61 protected void Button1_Click(object sender, EventArgs e)
62 {
63 CheckBox2.Checked = false;
64 for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
65 {
66 CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
67 cbox.Checked = false;
68 }
69 }
70
71 public void bind()
72 {
73 string sqlstr = "select top 5 * from table1";
74 sqlcon = new SqlConnection(strCon);
75 SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
76 DataSet myds = new DataSet();
77 sqlcon.Open();
78 myda.Fill(myds, "tb_Member");
79 GridView1.DataSource = myds;
80 GridView1.DataKeyNames = new string[] { "身份证号码" };
81 GridView1.DataBind();
82 sqlcon.Close();
83 }
84}
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Data.SqlClient;
11
12public partial class Default5 : System.Web.UI.Page
13{
14 SqlConnection sqlcon;
15 string strCon = "Data Source=(local);Database=db1;Uid=sa;Pwd=sa";
16
17 protected void Page_Load(object sender, EventArgs e)
18 {
19 if (!IsPostBack)
20 {
21 bind();
22 }
23 }
24
25 protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
26 {
27 for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
28 {
29 CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
30 if (CheckBox2.Checked == true)
31 {
32 cbox.Checked = true;
33 }
34 else
35 {
36 cbox.Checked = false;
37 }
38 }
39 }
40
41 protected void Button2_Click(object sender, EventArgs e)
42 {
43 sqlcon = new SqlConnection(strCon);
44 SqlCommand sqlcom;
45 for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
46 {
47 CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
48 if (cbox.Checked == true)
49 {
50
51 string sqlstr = "delete from table1 where 身份证号码='" + GridView1.DataKeys[i].Value + "'";
52 sqlcom = new SqlCommand(sqlstr, sqlcon);
53 sqlcon.Open();
54 sqlcom.ExecuteNonQuery();
55 sqlcon.Close();
56 }
57 }
58 bind();
59 }
60
61 protected void Button1_Click(object sender, EventArgs e)
62 {
63 CheckBox2.Checked = false;
64 for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
65 {
66 CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
67 cbox.Checked = false;
68 }
69 }
70
71 public void bind()
72 {
73 string sqlstr = "select top 5 * from table1";
74 sqlcon = new SqlConnection(strCon);
75 SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
76 DataSet myds = new DataSet();
77 sqlcon.Open();
78 myda.Fill(myds, "tb_Member");
79 GridView1.DataSource = myds;
80 GridView1.DataKeyNames = new string[] { "身份证号码" };
81 GridView1.DataBind();
82 sqlcon.Close();
83 }
84}
前台主要代码:
Code
1<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
2 CellPadding="3" Font-Size="9pt" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
3 BorderWidth="1px">
4 <FooterStyle BackColor="White" ForeColor="#000066" />
5 <Columns>
6 <asp:TemplateField>
7 <ItemTemplate>
8 <asp:CheckBox ID="CheckBox1" runat="server" />
9 </ItemTemplate>
10 </asp:TemplateField>
11 <asp:BoundField DataField="身份证号码" HeaderText="用户ID" SortExpression="身份证号码" />
12 <asp:BoundField DataField="姓名" HeaderText="用户姓名" SortExpression="姓名" />
13 <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" SortExpression="家庭住址" />
14 </Columns>
15 <RowStyle ForeColor="#000066" />
16 <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
17 <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
18 <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
19</asp:GridView>
20
21<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" Font-Size="9pt" OnCheckedChanged="CheckBox2_CheckedChanged" Text="全选" />
22<asp:Button ID="Button1" runat="server" Font-Size="9pt" Text="取消" OnClick="Button1_Click" />
23<asp:Button ID="Button2" runat="server" Font-Size="9pt" Text="删除" OnClick="Button2_Click" />
1<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
2 CellPadding="3" Font-Size="9pt" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
3 BorderWidth="1px">
4 <FooterStyle BackColor="White" ForeColor="#000066" />
5 <Columns>
6 <asp:TemplateField>
7 <ItemTemplate>
8 <asp:CheckBox ID="CheckBox1" runat="server" />
9 </ItemTemplate>
10 </asp:TemplateField>
11 <asp:BoundField DataField="身份证号码" HeaderText="用户ID" SortExpression="身份证号码" />
12 <asp:BoundField DataField="姓名" HeaderText="用户姓名" SortExpression="姓名" />
13 <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" SortExpression="家庭住址" />
14 </Columns>
15 <RowStyle ForeColor="#000066" />
16 <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
17 <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
18 <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
19</asp:GridView>
20
21<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" Font-Size="9pt" OnCheckedChanged="CheckBox2_CheckedChanged" Text="全选" />
22<asp:Button ID="Button1" runat="server" Font-Size="9pt" Text="取消" OnClick="Button1_Click" />
23<asp:Button ID="Button2" runat="server" Font-Size="9pt" Text="删除" OnClick="Button2_Click" />