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);
}
}