GridView和CheckBox结合[转]

效果图:

图片

后台代码:

ContractedBlock.gif ExpandedBlockStart.gif 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
13ExpandedBlockStart.gifContractedBlock.gif{
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)
18ExpandedSubBlockStart.gifContractedSubBlock.gif    {
19        if (!IsPostBack)
20ExpandedSubBlockStart.gifContractedSubBlock.gif        {
21            bind();
22        }

23    }

24
25    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
26ExpandedSubBlockStart.gifContractedSubBlock.gif    {
27        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
28ExpandedSubBlockStart.gifContractedSubBlock.gif        {
29            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
30            if (CheckBox2.Checked == true)
31ExpandedSubBlockStart.gifContractedSubBlock.gif            {
32                cbox.Checked = true;
33            }

34            else
35ExpandedSubBlockStart.gifContractedSubBlock.gif            {
36                cbox.Checked = false;
37            }

38        }

39    }

40
41    protected void Button2_Click(object sender, EventArgs e)
42ExpandedSubBlockStart.gifContractedSubBlock.gif    {
43        sqlcon = new SqlConnection(strCon);
44        SqlCommand sqlcom;
45        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
46ExpandedSubBlockStart.gifContractedSubBlock.gif        {
47            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
48            if (cbox.Checked == true)
49ExpandedSubBlockStart.gifContractedSubBlock.gif            {
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)
62ExpandedSubBlockStart.gifContractedSubBlock.gif    {
63        CheckBox2.Checked = false;
64        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
65ExpandedSubBlockStart.gifContractedSubBlock.gif        {
66            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
67            cbox.Checked = false;
68        }

69    }

70
71    public void bind()
72ExpandedSubBlockStart.gifContractedSubBlock.gif    {
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;
80ExpandedSubBlockStart.gifContractedSubBlock.gif        GridView1.DataKeyNames = new string[] "身份证号码" };
81        GridView1.DataBind();
82        sqlcon.Close();
83    }

84}


前台主要代码:

ContractedBlock.gif ExpandedBlockStart.gif 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" />

 

转载于:https://www.cnblogs.com/qiangshu/archive/2009/10/29/1591846.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值