1、功能需求
在一个数据表格中,选中欲删除的记录,点击删除按钮,先触发前台验证,然后调用后台处理逻辑
2、代码实现
HTML代码:
<asp:Repeater ID="repInputList" runat="server" DataSourceID="InputsDataSource"
EnableViewState="true" >
<HeaderTemplate>
<table cellpadding="2" cellspacing="1" class="Admin_Table">
<thead>
<tr class="Admin_Table_Title">
<th style="width: 5%;" align="center">
选择
</th>
<th style="width: 6%;" align="center">
仓库
</th>
<th style="width: 6%;" align="center">
物资
</th>
<th style="width: 5%;" align="center">
数量
</th>
</tr>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tr onmouseover="this.style.backgroundColor=‘#ffff66‘;" onmouseout="this.style.backgroundColor=‘#d4e3e5‘;">
<td>
<asp:CheckBox ID="cb" runat="server" name="checkbox" />
<asp:HiddenField ID="hfrkid" runat="server" Value=‘<%# Eval("rkid")%>‘ />
</td>
<td title=‘<%#Eval("wname") %>‘>
<%#StringTruncat(Eval("wname").ToString(), 8, "...")%>
</td>
<td title=‘<%#Eval("gname") %>‘>
<%#StringTruncat(Eval("gname").ToString(), 4, "...")%>
</td>
<td>
<%#Eval("rkamount") %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table></FooterTemplate>
</asp:Repeater>
<asp:Button ID="btnAllDel" runat="server" Text="删除选中" OnClick="btnAllDel_Click" OnClientClick="return IFChecked();"
ForeColor="Red" />
JS代码:
//当函数返回false时,将不会调用后台的btnAllDel_Click()方法
function IFChecked(){
var count= $("input:checkbox:checked").length;
if (count<=0) {
alert("请选择要删除的记录!");
return false;
}else {
return confirm("您确认要删除选中的入库单吗?");
}
}
后台方法:
protected void btnAllDel_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.repInputList.Items.Count; i++)
{
RepeaterItem ri = this.repInputList.Items[i];
CheckBox chb = ri.FindControl("cb") as CheckBox;
string rkid = ((HiddenField)(ri.FindControl("hfrkid"))).Value;
if (chb.Checked == true)
{
inputdao.Delete(rkid);
}
}
Page.ClientScript.RegisterStartupScript(Page.GetType(), "MsgBox", "<script> alert(‘删除成功!‘)</script>");
rep_Bind();
}
删除JS前台校验,后台批量删除,布布扣,bubuko.com
原文地址:http://greensky.blog.51cto.com/3347654/1412079