现在的位置: 首页 > 综合 > 正文

DataGrid 中使用 复选框(CheckBox) 删除纪录

2012年06月03日 ⁄ 综合 ⁄ 共 1059字 ⁄ 字号 评论关闭

实现效果如图,点击删除后删除所选纪录

在DataGrid中加入模板列加入Label用以表示该纪录编号(id)

然后再用模板列制作删除列 插入复选框 checkbox

删除按钮的代码如下:

public void btnDel_Click(object sender,EventArgs e)
        
{
            
//要删除的id号
            string strId="";
            
foreach(DataGridItem i in DataGrid1.Items)
            
{
                
                CheckBox chk
=(CheckBox)i.FindControl("CheckBox1");
                
if(chk.Checked)
                
{
                    
//查找该CheckBox所对应纪录的id号,在Label2中
                    strId+=((Label)i.FindControl("Label2")).Text+",";                        
                }


            }

            
if(strId.Length>1)
            
{
                
//去掉最后的一个逗号
                strId=strId.Substring(0,strId.Length-1);
                
string sql="delete from Dif_Info where [id] in (" +strId + ")";
                db.RunProc(sql);
                Response.Write(
"<script language=javascript>window.alert('操作成功');</script>");
                BindGrid();
            }

            
        }

在html页中加入按钮方法

<asp:Button id="btnDel" onclick="btnDel_Click" runat="server" Text="删除"></asp:Button>

完成

抱歉!评论已关闭.