我有一个GirdView
,里面Girdview
有一个Checkbox
专栏。Checkbox
AutoPostback
属性设置为,TRUE
因为我正在对Checkbox
checked
状态进行一些数据库工作。现在我用BUt替换普通javascript confirm
框,问题是当用户出现对话框时,用户必须确认他们是否确定要选中复选框,如果他们按下yse,则选中复选框,然后我想要服务器端事件 t 触发。相反,一旦用户选中复选框,服务器端事件就会触发,然后即使他们按下 NO,也会出现对话框,数据库已经更新为 YES。Jquery
DialogBox
check
checkbox
这是我的代码。
HTML
<asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false"
CssClass="gvClickCollectOrders" DataKeyNames="OrderId" OnRowDataBound="gvOrders_RowDataBound"
AllowPaging="true" OnPageIndexChanging="gvOrders_PageIndexChanging">
<Columns>
<asp:BoundField DataField="OrderId" Visible="false" />
<asp:BoundField DataField="OrderNumber" HeaderText="Order No" DataFormatString="WWW {0}"
/>
<asp:TemplateField HeaderText="Order Date">
<ItemTemplate>
<asp:Label ID="orderDateText" Text="" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Order Ship Date">
<ItemTemplate>
<asp:HyperLink ID="orderShipDateAndTrackingData" CssClass="trackingInfo"
runat="server" Target="_blank" Text="" NavigateUrl=""></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Customer Name">
<ItemTemplate>
<%# Eval( "BillToFirstName") %>
<%# Eval( "BillToLastName") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Received In Store">
<ItemTemplate>
<asp:CheckBox ID="cbIsReceived" runat="server" AutoPostBack="true" Checked='<%# MWClickAndCollectHelper.CheckOrderReceivedStatus(AlwaysConvert.ToInt(Eval("OrderId"))) %>'
OnCheckedChanged="cbIsReceived_CheckedChanged" CssClass="isReceivedCheckBox"
/>
<asp:Label ID="receivedDateText" Text="" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Collected By Customer">
<ItemTemplate>
<asp:CheckBox ID="cbIsCollected" runat="server" AutoPostBack="true" Checked='<%# MWClickAndCollectHelper.CheckOrderCollectedStatus(AlwaysConvert.ToInt(Eval("OrderId"))) %>'
OnCheckedChanged="cbIsCollected_CheckedChanged" CssClass="isCollectedCheckBox"
/>
<asp:Label ID="collectedDateText" Text="" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<asp:Label ID="emptyGrid" runat="server" Text="There are no 'Click and Collect' orders placed for this store."
CssClass="emptyGridMessage"></asp:Label>
</EmptyDataTemplate>
</asp:GridView>
jQuery
< script type = "text/javascript" >
var isReceivedCheckBox = $('.isReceivedCheckBox input[type=checkbox]');
var isCollectedCheckBox = $('.isCollectedCheckBox input[type=checkbox]');
var def = $.Deferred();
function confirmDialog(msg) {
var dialog = $("<div id=\"dialog\">" + msg + "</div>");
$(dialog).dialog({
autoOpen: true,
width: 300,
height: 200,
resizable: false,
modal: false,
buttons: {
'Yes': function () {
def.resolve();
$(this).dialog("close");
},
'No': function () {
def.reject();
$(this).dialog("close");
}
},
close: function () {
$(this).remove();
}
});
return def.promise();
}
$(function () {
$(isReceivedCheckBox).on("change", function () {
var checked = this.checked;
var checkbox = this;
if (checked) {
confirmDialog("are your sure you want to check this checkbox?").done(function () {
checkbox.checked = true;
})
.fail(function () {
checkbox.checked = false;
});
} else {
confirmDialog("are your sure you want to uncheck this checkbox?").done(function () {
checkbox.checked = false;
})
.fail(function () {
checkbox.checked = true;
});
}
});
}); < /script>