GridView CheckBox 全选

时间:2022-06-18 21:27:14
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();

    }