GridView控件与CheckBox结合,实现…

  页面设计:
  1. <table style="border:solid 1px green" width="35%">
  2.             <tr>
  3.                 <td style="background-color:Fuchsia;">
  4.                     <strong>
  5.                         <span>
  6.                             GridView控件与checkbox控件结合
  7.                         </span>
  8.                     </strong>
  9.                 </td>
  10.             </tr>

  11.             <tr>
  12.                 <td >
  13.                     <asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" DataKeyNames="stuid">
  14.                         <Columns>
  15.                             <asp:TemplateField >   
  16.                                 <HeaderTemplate>
  17.                                     <asp:CheckBox ID="chAll" runat="server" Text="全选"  OnCheckedChanged="chAll_CheckedChanged" />
  18.                                 </HeaderTemplate>              
  19.                                            
  20.                                 <ItemTemplate>
  21.                                     <asp:CheckBox ID="cbo" runat="server" />
  22.                                 </ItemTemplate>
  23.                             </asp:TemplateField>
  24.                             <asp:BoundField DataField="stuid" HeaderText="学生编号" />
  25.                             <asp:BoundField DataField="stuname" HeaderText="姓名" />
  26.                             <asp:BoundField DataField="stuadd" HeaderText="地址" />
  27.                             <asp:BoundField DataField="stucity" HeaderText="城市" />
  28.                         </Columns>

  29.                         <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
  30.                         <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
  31.                         <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
  32.                       <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
  33.                       <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />

  34.                     </asp:GridView>                   

  35.                 </td>
  36.             </tr>

  37.             <tr>
  38.                 <td >
  39.                     <asp:Button ID="btndel" runat="server" Text="删除" OnClick="btndel_Click" />
  40.                     <asp:Button ID="btncancel" runat="server" Text="取消选择" OnClick="btncancel_Click" />
  41.                 </td>
  42.             </tr>
  43.         </table>  

  后代代码:
  1.     /// <summary>
  2.     /// 全选
  3.     /// </summary>
  4.     /// <param name="sender"></param>
  5.     /// <param name="e"></param>

  6.     protected void chAll_CheckedChanged(object sender, EventArgs e)
  7.     {
  8.         foreach (GridViewRow gr in GridView1.Rows)
  9.         {
  10.             CheckBox ch = (CheckBox)gr.FindControl("cbo");
  11.             //相互变幻
  12.             if (ch.Checked == true)
  13.             {
  14.                 ch.Checked = false;
  15.             }
  16.             else
  17.             {
  18.                 ch.Checked = true;
  19.             }
  20.         }
  21.     }

  22.     //取消选择
  23.     protected void btncancel_Click(object sender, EventArgs e)
  24.     {
  25.         foreach (GridViewRow gr in GridView1.Rows)
  26.         {
  27.             CheckBox ch = (CheckBox)gr.FindControl("cbo");
  28.             //取消选择
  29.             ch.Checked = false;
  30.         }
  31.     }

  32.     //删除
  33.     protected void btndel_Click(object sender, EventArgs e)
  34.     {
  35.         btndel.Attributes.Add("onclick","javascript:return confirm('确定删除吗?');");
  36.         //进行相关操作
  37.         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
  38.         con.Open();
  39.         //for(int i=0;i<=GridView1.Rows.Count-1;i++)
  40.         //{
  41.         foreach(GridViewRow gr in GridView1.Rows)
  42.         {            
  43.             CheckBox ch = (CheckBox)gr.FindControl("cbo");
  44.             if (ch.Checked == true)
  45.             {
  46.                 //定义删除行的字符串
  47.                 string cmdtext = "delete from student where 
  48. stuid="+GridView1.DataKeys[gr.RowIndex].Value.ToString();
  49.                 //创建命令对象
  50.                 SqlCommand cmd = new SqlCommand(cmdtext,con);
  51.                 //执行删除操作
  52.                 cmd.ExecuteNonQuery();
  53.             }
  54.         }
  55.         //关闭数据库连接
  56.         con.Close();
  57.         //回绑数据
  58.         BindGridView();
  59.         Response.Write("<script>alert('删除成功。');</script>");
  60.     }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值