欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET Gridview 中使用checkbox删除的2种方法实例分享

程序员文章站 2024-03-02 14:11:04
方法一:后台代码:复制代码 代码如下: protected void btn_delete_click(object sender, eventargs e)&n...
方法一:
后台代码:
复制代码 代码如下:

 protected void btn_delete_click(object sender, eventargs e)
    {
        for (int i = 0; i <this.gridview1.rows.count; i++)
        {
            int id = convert.toint32(this.gridview1.datakeys[i].value);
            if ((this.gridview1.rows[i].cells[0].findcontrol("checkbox1") as checkbox).checked == true)
            {
                delete(id);
                clientscript.registerstartupscript(gettype(),"提示","<script>alert('删除成功!')</script>");
            }
        }
        this.gridview1.databind();
    }//删除
    private void delete(int id)
    {
        using (sqlconnection conn = new sqlconnection(str))
        {
            conn.open();
            sqlcommand comm = conn.createcommand();
            comm.commandtext = "delete from notice_msg where id=@id";
            comm.parameters.add(new sqlparameter("@id", id));
            comm.executenonquery();
        }
    }

前台代码:
复制代码 代码如下:

<asp:gridview id="gridview1" runat="server" datakeynames="id">

另外还得添加一列,让其绑定的字段为id,并且把这一列的visable属性设为false
方法二:
后台:
复制代码 代码如下:

 protected void btn_delete_click(object sender, eventargs e)
    {
        foreach (gridviewrow row in this.gridview1.rows)
        {
            if (row.rowtype == datacontrolrowtype.datarow)
            {
                checkbox ckb = row.cells[2].findcontrol("checkbox1") as checkbox;
                if (ckb.checked)
                {
                    using (sqlconnection sqlcnn = new sqlconnection(str))
                    {
                        using (sqlcommand sqlcmm = sqlcnn.createcommand())
                        {
                            sqlcmm.commandtext = "delete from regime_table where id='" + row.cells[0].text + "' ";
                            sqlcnn.open();
                            int a= sqlcmm.executenonquery();
                            if (a>0)
                            {
                                clientscript.registerstartupscript(gettype(),"提示","<script>alert('删除成功!')</script>");
                            }
                            else
                            {
                                clientscript.registerstartupscript(gettype(), "提示", "<script>alert('删除失败!')</script>");
                            }
                            this.databind();
                        }
                    }
                }
            }
        }
    }

前台:
复制代码 代码如下:

<style type="text/css">
    .hidden
    {
        display:none;
    }
    </style>
<asp:boundfield datafield="id" headertext="编号" >
                   <headerstyle cssclass="hidden" />
                   <itemstyle cssclass="hidden" />
                   </asp:boundfield>

新增加一列,这一列绑定id字段,并且visable属性不能为false,否则取不出值来。

checkbox全选功能:
复制代码 代码如下:

<script type="text/jscript">
         function change(sender) {
             var table = document.getelementbyid("gridview1");
             for (var i = 1; i < table.rows.length; i++) {
                 table.rows[i].cells[1].getelementsbytagname("input")[0].checked = sender.checked;
             }
         }
    </script>
<headertemplate>
      <input id="checkbox2" type="checkbox" onclick="change(this)"/>
             全选
         </headertemplate>