GridView CheckBox 全选

GridView CheckBox 全选
    

    <script type="text/javascript">

        $(function () {

            $("#allCheck").click(function () {  //点击全选按钮

                if ($(this).prop("checked")) {

                    $("#GridView1 :checkbox").prop("checked", true);

                } else {

                    $("#GridView1 :checkbox").prop("checked", false);

                }

            });

 

            $("#GridView1 :checkbox:gt(0)").click(function () {

                var chItem = $("#GridView1 :checkbox:gt(0)");

                var isAllCheck = true;//是否全部选中了

                for (var i = 0; i < chItem.length; i++) {

                    if (!$(chItem[i]).prop("checked")) {

                        isAllCheck = false;

                        break;

                    }

                }

                $("#allCheck").prop("checked", isAllCheck);

            });

        });

    </script>

 

            <asp:GridView ID="GridView1" runat="server" CssClass="dataTable" DataKeyNames="ID">

                <Columns>

                    <asp:TemplateField>

                        <HeaderTemplate>

                            <input type="checkbox" id="allCheck" />

                        </HeaderTemplate>

                        <ItemTemplate>

                            <asp:CheckBox ID="CheckBox1" runat="server" />

                        </ItemTemplate>

                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="操作">

                        <ItemTemplate>

                            <a href="CreateCompanyShop.aspx?cm=<%#Eval("COMPANY") %>" title="详情">

                                <img src="../images/明细.png" width="20" title="详情" height="20" border="0" /></a>

                        </ItemTemplate>

                    </asp:TemplateField>

                </Columns>

            </asp:GridView>

    

 

        // 获取选中的ID集合

        private List<string> GetCheckRowIds()

        {

            //获取复选框被选中的行id

            List<string> lst = new List<string>();

            foreach (GridViewRow row in GridView1.Rows)

            {

                CheckBox cb = row.Cells[0].FindControl("CheckBox1") as CheckBox;

                if (cb.Checked)

                {

                    lst.Add(GridView1.DataKeys[row.RowIndex].Value.ToString());

                    //ids += "‘" + GridView1.DataKeys[row.RowIndex].Value + "‘,";

                }

            }

            return lst;

        }

    

    

        public bool DeleteCMShopByIdList(List<string> idList)

        {

            string ids = string.Empty;

            foreach (string item in idList)

            {

                ids += "‘" + item + "‘,";

            }

            ids = ids.Trim(‘,‘);

            string sql = "DELETE Company_Shop WHERE ID  IN(" + ids + ");";

            SqlTransaction tran = dbhelper.GetTransAction();

            try

            {

                dbhelper.ExcuteNonequery(sql, tran);

                tran.Commit();

                return true;

            }

            catch (Exception)

            {

                tran.Rollback();

            }

            finally

            {

                tran.Dispose();

            }

            return false;

        }

        

 

 

 

   //TWO

    private string GetCheckRowIds()

    {

        //获取复选框被选中的行id

        string ids = string.Empty;

        foreach (GridViewRow row in GridView1.Rows)

        {

            CheckBox cb = row.Cells[0].FindControl("CheckBox1") as CheckBox;

            if (cb.Checked)

            {

                ids += "" + GridView1.DataKeys[row.RowIndex].Value + ",";

            }

        }

        if (ids != string.Empty)

        {

            ids = ids.TrimEnd(‘,‘);

        }

        return ids;

    }        

        

        

    protected void btnSure_Click(object sender, EventArgs e)

    {

        string ids = GetCheckRowIds();

        if (ids == string.Empty)

        {

            Page.ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script>alert(‘你没有选择任何选项‘)</script>");

            return;

        }

        int undoCount = 0;

        string[] idArray = ids.Split(‘,‘);

        IDbTransaction tran = ConnectStringConfig.GetTran();

        try

        {

            foreach (string id in idArray)

            {

                string strStatus = "";

                string sql = "select ID,TYPE,STATUS from tasks_direct where id = " + id + "";

                DataTable dttask = dbHelper.GetDataTable(sql);

                foreach (DataRow dr in dttask.Rows)

                {

                    strStatus = dr["STATUS"].ToString();

                    if (strStatus == "00")

                    {

                        boTaskDirect.ConfirmMove(dr["ID"].ToString(), boTaskDirect.TblTaskDirect.WEIGHT.Value, true, tran);

                        undoCount++;

                    }

                }

            }

            tran.Commit();

        }

        catch (Exception ex)

        {

            tran.Rollback();

            Response.Redirect(SysConfig.ErrorPage + ex.Message);

        }

        finally

        {

            tran.Dispose();

        }

        string message = undoCount.ToString() + " 个任务确认成功!";

        Page.ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script>alert(‘" + message + "‘)</script>");

        Paginationer.BindData();

    }

GridView CheckBox 全选

时间: 2024-07-28 22:50:22

GridView CheckBox 全选的相关文章

关于input[type=&#39;checkbox&#39;]全选的问题

今天在做一个全选功能的时候,发现了一个问题,就是如果我在选择全选之前,我就已经选择了一个input,然后我再去选择全选并且以后再取消全选的时候,这个我之前选择的input始终处于选择状态,但是他的checked的值一直是在true和false之间变化,当checked=false的时候,仍然是被选中的.到现在还没处理好这个问题.希望看到的哪位大神能给个好的解决办法,实在感激.下面是类似的代码. <!DOCTYPE html><html lang="en"><

利用jQuery实现CheckBox全选/全不选/反选

转自:http://www.cnblogs.com/linjiqin/p/3148259.html jQuery有些版本中实现CheckBox全选/全不选/反选会有bug,经测试jquery-1.3.1.js–>测试通过,jquery-1.5.1.js–>测试不通过. 实现CheckBox全选/全不选/反选代码如下: <%@ page language="java" pageEncoding="UTF-8"%>   <!DOCTYPE

js初学—实现checkbox全选功能

布局如下: <p ><input type="checkbox" id="che1"/>全选</p><div id="div1"> <input type="checkbox" /><br /> <input type="checkbox" /><br /> <input type="checkb

checkbox 全选,反选 ,全不选

在表格或者列表中经常会遇到要全选或者反选等交互,今天总结了一下代码,保留着以后直接拿来用 原理: 1. 全选:当全选checkbox被点击(不管点击之前是什么状态)后,获取其checked状态.然后对列表进行循环检测,此时可以将所有的(无论之前什么状态),设为选中,也可对未选中的进行选中. 2. 反选:当反选checkbox被点击(不管点击之前是什么装填)后,获取其其状态值,对列表进行循环检测,将被检测的元素的checked状态反向处理,即可. 3. 列表全选或者不全选:当列表中的任意一个che

jQuery设置checkbox全选(区别jQuery版本)

jQuery设置checkbox全选在网上有各种文章介绍,但是为什么在我们用他们的代码的时候就没有效果呢? 如果你的代码一点错误都没有,先不要急着怀疑人家代码的正确性,也许只是人家跟你用的jQuery版本不同而已. jQuery很多版本都会对一些小的功能做一些改进,比如checkbox的选中. jQuery对checkbox改动的界线版本 jquery1.9.1. jquery1.9.1之前,全选是这样的: $('#checkbox').attr('checked',true) $('#chec

Jquery 组checkbox全选checkbox

<!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8"> <title></title> <style> </style></head><body><form action=""> <input type="checkbox&quo

表单javascript checkbox全选 反选 全不选

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>javascript checkbox全选 反选 全不选</title> <meta name="des

jQuery --checkbox全选和取消全选简洁高效的解决办法

最近在公司做了一个小项目,其中有一个全选和取消全选的这么一个模块,搞了半天找不到一种最佳的解决方案!后来通过各种努力找到了一种简洁高效的解决办法,这里想和大家分享一下.有问题的话,还望各路大神指导一二. html代码如下: <fieldset data-role="controlgroup">  <label><input type="checkbox" name="boxes" id="select_al

jQuery实现CheckBox全选、全不选

1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head runat="server"> 4 &