GridView CheckBox 批量删除
gridview里面放了checkbox来实现批量删除,现在发现一个问题,就是我在第一页选中一项,第二页选中一项,点击删除,只能删除第二页我选中的那个,第一页选中的没有删除。
删除按钮代码
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; } }
//判断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; }
<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;" />
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"): try { GVdata.PageIndex = (GVdata.PageCount - 1); } catch { GVdata.PageIndex = 0; } break; default: //本页值 GVdata.PageIndex = 0; break; } GVdataDataBind();
private string SelectID { get { object obj = this.ViewState["SelectID"]; return ViewState["SelectID"] == null ? "" : obj.ToString(); } set { ViewState["SelectID"] = value; } }SelectID+="你勾选的ID"+",";