標籤:js 前台驗證 後台刪除
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();
}