<asp:CheckBox ID="chbAll" runat="server" Text="全选" onclick="GetAll(this)"/>
<script language="javascript" type="text/javascript">
function GetAll(obj)
{
var item=document.gerElementByTagName("input"); //他获取的东西是一个集合,所以可以用for循环
for(i=0;i<item.length;i++)
{
if(item[i].type=="checkbox")
{
item[i].checked=obj.checked;
}
}
}
<asp:CheckBox ID="chbSelect" runat="server" />
<asp:HiddenField runat="server" ID="hfId" Value='<%#Eval("Id") %>' />// 通过隐藏域来保存每一条需要删除的ID
上面的js代码仅仅是选择了一个页面的所有checkbox;
下面的cs代码则是实现批量删除:每一个checkbox和隐藏域是放在repeat中的
protected void btnDelSelect_Click(object sender, EventArgs e)
{
int count = 0;
string ch = string.Empty;//存放选择的ID
string jch = string.Empty;//截取后的ID
foreach(RepeaterItem ri in repAll.Items)
{
CheckBox cb=ri.FindControl("chbSelect") as CheckBox;
HiddenField hid=ri.FindControl("hfid") as HiddenField;
if(cb.Checked==true)
{
count++;
ch+=hid.Value.ToString()+",";
jch=ch.Substring(0,ch.LastIndexOf(','));
}
if (count > 0)
{
bool flag = new VideosBLL().DeleteVideoMember(jch);
if (flag)
{
PageHelper.ShowMsgBox(this, "删除成功!", PageHelper.MsgStatus.succeed, 1);
GetDocumentUpLoad();
}
else
{
PageHelper.ShowMsgBox(this, "删除失败!");
}
}
if (count == 0)
{
PageHelper.ShowMsgBox(this, "请选择您要删除的数据!", PageHelper.MsgStatus.question, 0);
}
}
}
/// <summary>
/// 批量删除用户
/// </summary>
/// <param name="ids">用户Ids</param>
/// <returns></returns>
public bool DeleteVideoMember(string id)
{
string strWhere = "Id in(" + id + ")";
bool flag = GetPagedRecord.ModifyData("Videos", "IsDelete", "1", strWhere, 1);
return flag;
}
/// <param name="tbName">表名</param>
/// <param name="modifyField">字段名</param>
/// <param name="modifyData">字段值</param>
/// <param name="strWhere">where条件,不加where</param>
/// <param name="isUpdateTime">是否更新时间(须有updateTime字段才可以)</param>
/// <returns></returns>
public static bool ModifyData(string tbName, string modifyField, string modifyData, string strWhere, int isUpdateTime)
{
bool flag = false;
SqlParameter[] paras = new SqlParameter[] {
new SqlParameter("@modifyData",SqlDbType.NVarChar,2000)
};
paras[0].Value = modifyData;
string sql = "update " + tbName + " set " + modifyField + "=@modifyData " + (isUpdateTime == 1 ? " ,UpdateTime=getdate()" : "") + (string.IsNullOrEmpty(strWhere) ? "" : " where " + strWhere);
int result = SqlHelper.ExecuteNonQuery(SqlHelper.MainConnectionString, CommandType.Text, sql, paras);
if (result > 0)
{
flag = true;
}
return flag;
}