日期:2014-05-17  浏览次数:20392 次

GridView CheckBox 批量删除
gridview里面放了checkbox来实现批量删除,现在发现一个问题,就是我在第一页选中一项,第二页选中一项,点击删除,只能删除第二页我选中的那个,第一页选中的没有删除。
删除按钮代码
C# code
protected void igb_delete_Click(object sender, ImageClickEventArgs e)
        {
            string IDlist = Common.PublicMethod.CheckCbx(this.GVdata, "chb_delete", "LabVisible");

            if (IDlist.Trim() != "")
            {
                if (!Warningbll.DeleteList(IDlist))
                {
                    Response.Write("<script>alert('删除选中记录时发生错误!请重新登陆后重试!');</script>");
                }
                else
                {
                    GVdataDataBind();
                }
            }
            else
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), " alert('请选择你要删除的项!'); ", true);
                return;
            }

        }
C# code
//判断GridView里面被选中的ID
        public static string CheckCbx(GridView GVData, string CheckBoxName, string LabID)
        {
            string str = "";
            for (int i = 0; i < GVData.Rows.Count; i++)
            {
                GridViewRow row = GVData.Rows[i];
                CheckBox Chk = (CheckBox)row.FindControl(CheckBoxName);
                Label LabVis = (Label)row.FindControl(LabID);
                if (Chk.Checked == true)
                {
                    if (str == "")
                    {
                        str = LabVis.Text.ToString();
                    }
                    else
                    {
                        str = str + "," + LabVis.Text.ToString();
                    }
                }
            }
            return str;
        }
前台分页代码
HTML code
<asp:ImageButton ID="BtnFirst" runat="server" CommandName="First" ImageUrl="../../images/Button/First.jpg"
                                        OnClick="PagerButtonClick" Style="height: 14px" />
                                    <asp:ImageButton ID="BtnPre" runat="server" CommandName="Pre" ImageUrl="../../images/Button/Pre.jpg"
                                        OnClick="PagerButtonClick" Style="height: 14px" />
                                    <asp:ImageButton ID="BtnNext" runat="server" CommandName="Next" ImageUrl="../../images/Button/Next.jpg"
                                        OnClick="PagerButtonClick" Style="width: 16px" />
                                    <asp:ImageButton ID="BtnLast" runat="server" CommandName="Last" ImageUrl="../../images/Button/Last.jpg"
                                        OnClick="PagerButtonClick" Style="height: 14px; width: 16px;" />
后台分页代码
C# code
string arg = ((ImageButton)sender).CommandName.ToString();
            switch (arg)
            {
                case ("Next"):
                    if (this.GVdata.PageIndex < (GVdata.PageCount - 1))
                        GVdata.PageIndex++;
                    break;
                case ("Pre"):
                    if (GVdata.PageIndex > 0)
                        GVdata.PageIndex--;
                    break;
                case ("Last"):