[英]How to delete a checkbox with a delete button
我試圖刪除帶有刪除按鈕(Deletebtn)onClick Delete Selected的復選框,我在datagrid表中有兩個復選框,分別是標題chkAll和項目chkRow。
Sub DeleteSelected(sender as object, e as System.EventArgs)
Dim item As DataGridItem
Dim SQLRemoveItem As String
For Each item In OrderForm.Items
Dim selection As CheckBox = _
DirectCast(item.Cells(12).FindControl("chkRow"), CheckBox)
If selection IsNot Nothing
' AndAlso selection.Checked then
Dim SQLCheck As String = "select id, isbn from order_detail where ordernumber='{0}' and isbn ='{1}'"
Dim Check As DataTable = Database.SelectRows(String.Format(SQLCheck, OrderNumber.Text, Item.cells(3).Text))
SQLRemoveItem = "Delete from order_detail where id = '{0}'"
Database.InsertRecord(String.Format(SQLRemoveItem, Check.Rows(0)("id")))
End if
Next Item
BindOrderForm() 'Rebind data after delete
End Sub
<script type="text/javascript">
$("[id*=chkAll]").live("click", function () {
var chkAll = $(this);
var grid = $(this).closest("table");
$("input[type=checkbox]", grid).each(function () {
if (chkAll.is(":checked")) {
$(this).attr("checked", "checked");
$("td", $(this).closest("tr")).addClass("selected");
} else {
$(this).removeAttr("checked");
$("td", $(this).closest("tr")).removeClass("selected");
}
});
});
$("[id*=chkRow]").live("click", function () {
var grid = $(this).closest("table");
var chkAll = $("[id*=chkAll]", grid);
if (!$(this).is(":checked")) {
$("td", $(this).closest("tr")).removeClass("selected");
chkAll.removeAttr("checked");
} else {
$("td", $(this).closest("tr")).addClass("selected");
if ($("[id*=chkRow]", grid).length == $("[id*=chkRow]:checked", grid).length) {
chkAll.attr("checked", "checked");
}
}
});
</script>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:CheckBox id="chkAll" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox id="chkRow" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
我的問題是,當我嘗試搜索chkRow並刪除所選的chkrow框時。 它會刪除所有內容!
如果我應該添加選擇,則選中它不會執行任何操作,不確定我哪里出錯了。 請幫助!
您的代碼將刪除所有內容,因為您將遍歷所有行,檢查是否存在復選框,然后刪除該行。 由於您具有復選框列,因此將始終找到該復選框。 我可以看到您的評論行,因此我了解您的嘗試。
我認為這是您應該做的:
調試代碼並獲取item.Cells(12)的值。 我確實認為應該將其設置為“ true”或“ false”。 您不必強制轉換單元格即可獲取值。 我敢打賭,item.Cells(12).ToString()剛好足以獲取它。
然后創建一個合適的if語句,如下所示:
If item.Cells(12).ToString() = "true"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.