1、GridView 行的多选
<asp:TemplateField ControlStyle-Width="30" HeaderText="选择" > <ItemTemplate> <asp:CheckBox ID="DeleteThis" onclick="javascript:CCA(this);" runat="server" /> </ItemTemplate> </asp:TemplateField> private string GetSelIDlist() { string idlist = ""; bool BxsChkd = false; for (int i = 0; i < gridView.Rows.Count; i++) { CheckBox ChkBxItem = (CheckBox)gridView.Rows[i].FindControl("DeleteThis"); if (ChkBxItem != null && ChkBxItem.Checked) { BxsChkd = true; //#warning 代码生成警告:请检查确认Cells的列索引是否正确 if (gridView.DataKeys[i].Value != null) { idlist += gridView.DataKeys[i].Value.ToString() + ","; } } } if (BxsChkd) { idlist = idlist.Substring(0, idlist.LastIndexOf(",")); } return idlist; }
/// <summary> /// 批量删除数据 /// </summary> public bool DeleteList(string Idlist ) { StringBuilder strSql=new StringBuilder(); strSql.Append("delete from General_KnowledgePoint "); strSql.Append(" where Id in ("+Idlist + ") "); int rows=DbHelperSQL.ExecuteSql(strSql.ToString()); if (rows > 0) { return true; } else { return false; } }
方式二:
<script type="text/JavaScript">
function CheckBoxAll() {
if ($("input[id*='ContentPlaceHolder1_GVListShow_chkAllProp']").attr("checked") == true) {
$("input[id*='chkSelect']").attr("checked", true);
}
else {
$("input[id*='chkSelect']").attr("checked", false);
}
}
</script>
<asp:TemplateField HeaderText="选择">
<HeaderTemplate>
<input type="checkbox" id="chkAllProp" runat="server" onclick="CheckBoxAll()" />全选
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" Text="选择" />
</ItemTemplate>
</asp:TemplateField>
//取主键值
string GetIndexKey()
{
string indexKey = "";
for (int i = 0; i <= GVListShow.Rows.Count - 1; i++)
{
CheckBox cbox = (CheckBox)GVListShow.Rows[i].FindControl("chkSelect");
if (cbox.Checked == true)
{
if (indexKey == "")
{
indexKey = GVListShow.Rows[i].Cells[0].Text.Trim();
}
else
{
indexKey += "," + GVListShow.Rows[i].Cells[0].Text.Trim();
}
}
}
return indexKey;
}
相关实例: //批量删除 protected void btnDelect_Click(object sender, EventArgs e) { string indexKeys = GetIndexKey(); if (indexKeys != "") { string strWhere = string.Format("Id in ({0})", indexKeys); DotNet.BLL.Web_Article BLL = new DotNet.BLL.Web_Article(); if (1 <= BLL.DeleteWhere(strWhere, null)) { Web_ArticleBind(); } } } protected void btnTop_Click(object sender, EventArgs e) { string indexKeys = GetIndexKey(); if (indexKeys != "") { DotNet.BLL.Web_Article BLL = new DotNet.BLL.Web_Article(); int maxTopNum = 0; string strGetWhere = "ComanyId = " + USER.SchoolId; object selMaxTopNum = BLL.GetSingle("Max(OrderNum)", strGetWhere, null); string strWhere = string.Format("Id in ({0})", indexKeys); if (selMaxTopNum != null) { maxTopNum = int.Parse(selMaxTopNum.ToString()) + 1; if (1 <= BLL.Update("OrderNum = " + maxTopNum, strWhere, null)) { Web_ArticleBind(); } } } } protected void btnCancelTop_Click(object sender, EventArgs e) { string indexKeys = GetIndexKey(); if (indexKeys != "") { DotNet.BLL.Web_Article BLL = new DotNet.BLL.Web_Article(); int maxTopNum = 0; //object selMaxTopNum = BLL.GetSingle("Max(top_num)", "", null); string strWhere = string.Format("Id in ({0})", indexKeys); if (1 <= BLL.Update("OrderNum = " + maxTopNum, strWhere, null)) { Web_ArticleBind(); } } } //审核新闻 protected void btnPass_Click(object sender, EventArgs e) { string indexKeys = GetIndexKey(); if (indexKeys != "") { DotNet.BLL.Web_Article BLL = new DotNet.BLL.Web_Article(); string strWhere = string.Format("Id in ({0})", indexKeys); if (1 <= BLL.Update("StateNum = 2,StateName='已审核' ", strWhere, null)) { //Response.Redirect(Request.Url.ToString()); Web_ArticleBind(); } } } //取消审核 protected void btnUnpass_Click(object sender, EventArgs e) { string indexKeys = GetIndexKey(); string strUpdate = "SavedOn=getdate()"; if (indexKeys != "") { DotNet.BLL.Web_Article BLL = new DotNet.BLL.Web_Article(); string strWhere = string.Format("Id in ({0})", indexKeys); if (1 <= BLL.Update(strUpdate+",StateNum = 1,StateName='未审核'", strWhere, null)) { //Web_NewsBind(); //Response.Redirect(Request.Url.ToString()); Web_ArticleBind(); } } } //转移新闻栏目 protected void btnChangeCategory_Click(object sender, EventArgs e) { if (DdlCategoryIdNew.SelectedValue.Trim() == "0") { DotNet.Common.MessageBox.Show(this, "请选择栏目"); return; } string indexKeys = GetIndexKey(); if (indexKeys != "") { DotNet.BLL.Web_Article BLL = new DotNet.BLL.Web_Article(); string strField = "CategoryId = @CategoryId,CategoryName = @CategoryName"; Hashtable ht = new Hashtable(); ht.Add("@CategoryId", DdlCategoryIdNew.SelectedValue.Trim()); ht.Add("@CategoryName", DdlCategoryIdNew.SelectedItem.Text.Trim()); string strWhere = string.Format("Id in ({0})", indexKeys); if (1 <= BLL.Update(strField, strWhere, ht)) { Web_ArticleBind(); } } }