ASP.NET全选或多选删除
在文件中要先拖入jQuery.js文件
<script type="text/javascript" language="javascript">
//全选
$(function() {
$("#cbAll").click(function() {
$('input[name="cb"]').attr("checked", this.checked);
});
var $cb = $("input[name='cb']");
$cb.click(function() {
$("#cbAll").attr("checked", $cb.length == $("input[name='cb']:checked").length ? true : false);
});
var isCheched = $("#cbAll").attr("checked");
var idStr = "";
$("[name='cb']").each(function() {
$(this).attr("checked", isCheched);
idStr = idStr + "," + $(this).val();
});
$("#" + '<%=hfids.ClientID %>').val(idStr);
});
//多选
$(function() {
$("[name='cb']").click(function() {
var idStr = "";
$("[name='cb']:checked").each(function() {
idStr = idStr + "," + $(this).val();
});
$("#" + '<%=hfids.ClientID %>').val(idStr);
});
});
</script>
要重复的区域的多选框
<input type="checkbox" name="cb" value='<%#Eval("id") %>' />
分页旁边的选择框
<input type="checkbox" id="cbAll" />全选 //全选的复选框,勾选时是全选,不勾选时是全不选
<asp:LinkButton ID="lbtnAllDel" runat="server" OnClick="lbtnAllDel_Click">删除</asp:LinkButton> //LinkButton删除按钮
<asp:HiddenField ID="hfids" runat="server" /> //隐藏域用于存放选择的id值
多项选择删除后台事件
protected void lbtnAllDel_Click(object sender, EventArgs e)
{
string ids = hfids.Value; //1,2,3,4,5,6
string[] ss = ids.Split(','); //用,号隔开的数值 1 2 3 4 5 6
foreach (string str in ss)
{
int x;
if (int.TryParse(str,out x)) //当1 2 3 4 5 6 是数字存放于x中
{
new XiaobinShop.DAL.NewsDAO().Delete(x);
}
Xiaobin.Utility.Tool.AlertAndGo("删除成功",Request.Url.ToString(),this.Page);
}
}
直接后台方法
全选框
<asp:CheckBox ID="chkall" AutoPostBack="true" runat="server" oncheckedchanged="chkall_CheckedChanged" />
重复区域的的复选框
<asp:CheckBox ID="chk" ToolTip='<%#Eval("id") %>' runat="server" />
后台:
protected void chkall_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (sender as CheckBox);
if (chk.Checked)
{
foreach (RepeaterItem item in rep.Items)
{
(item.FindControl("chk") as CheckBox).Checked = true;
}
}
else
{
foreach (RepeaterItem item in rep.Items)
{
(item.FindControl("chk") as CheckBox).Checked = false;
}
}
}
删除事件 删除按钮在rep外面的
protected void Button3_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in rep.Items)
{
CheckBox chk = item.FindControl("chk") as CheckBox;
if (chk.Checked)
{
dal.Delete(int.Parse(chk.ToolTip));
}
}
Response.Redirect(Request.Url.ToString());
}
编辑 编辑按钮在rep外面的
protected void btnMod_Click(object sender, EventArgs e)
{
hfoperate.Value = "edit";
string id = "";
foreach (RepeaterItem item in rep.Items)
{
CheckBox chk = item.FindControl("chk") as CheckBox;
if (chk.Checked)
{
id = chk.ToolTip;
break;
}
}
if (id == "")
{
Xiaobin.Utility.Tool.Alert("请选择需要编辑的项。", this.Page);
return;
}
Model.Class c = dal.GetModel(int.Parse(id));
if (c != null)
{
txtclassname.Text = c.classname;
hfid.Value = c.id.ToString();
Xiaobin.Utility.Tool.ExecJS("$.popup.open('#pop')", this.Page);
}
}