在asp.net中选中复选框时弹出窗口
本文关键字:窗口 asp net 复选框 | 更新日期: 2023-09-27 17:57:34
当用户检查CheckBox
时,我想显示一个带有消息的confirmation Box
所以我有一个GridView
,还有一列是CheckBoxes
因此,每当用户选中CheckBox
时,我都希望显示一个确认框,当用户单击该框上的取消时,我希望取消选中该CheckBox
当用户按下OK时,我想启动一个标准的asp.net CheckBox_CheckedChanged
,我正在那里做一些数据库工作
我不知道如何在javascript
或Jquery
中做到这一点
我在谷歌上找到了它,那里只有一个CheckBox
,你可以使用ID
,使用Jquery
你可以显示弹出窗口
但是我有一个GridView,每行有很多CheckBoxes
。
请给我推荐一些工作示例或代码。
感谢
******编辑这是我到目前为止得到的代码。
$('#gvOrders').click(function () {
var checked = $(this).is(':checked');
if (checked) {
document.getElementById("confirm_value").value = "Yes";
if (!confirm('Are you sure you want to mark this order as received?')) {
$(this).removeAttr('checked');
}
}
else {
document.getElementById("confirm_value").value = "No";
if (!confirm('Are you sure you want to mark this order as not received?')) {
$(this).removeAttr('checked');
}
});
到目前为止,当CheckBox
被选中时,这是不起作用的。我不确定我在这里做错了什么。
***HTML FOR GRIDWIEW******
<asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" CssClass="gvClickCollectOrders"
DataKeyNames="ac_OrderId" OnRowDataBound="gvOrders_RowDataBound" AllowPaging="true">
<Columns>
<asp:BoundField DataField="ac_OrderId" Visible="false" />
<asp:BoundField DataField="ac_OrderNumber" HeaderText="Order No" DataFormatString="WWW{0}" />
<asp:TemplateField HeaderText="Order Date">
<ItemTemplate>
<%# GetOrderDate(AlwaysConvert.ToInt(Eval("ac_OrderId"))) %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Customer Name">
<ItemTemplate>
<%# Eval("CustomerFirstName") %> <%# Eval("CustomerLastName") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Received In Store">
<ItemTemplate>
<asp:CheckBox ID="cbIsReceived" runat="server" AutoPostBack="true" Checked='<%# Eval("IsReceived") %>'
OnCheckedChanged="cbIsReceived_CheckedChanged"/>
<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='<%# Eval("IsCollected") %>'
OnCheckedChanged="cbIsCollected_CheckedChanged" />
<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 the selected store."
CssClass="emptyGridMessage"></asp:Label>
</EmptyDataTemplate>
</asp:GridView>
使用jQuery
将某个类分配给gridview复选框,并将事件绑定到该类上。
<asp:CheckBox id="chkChoice" runat="server" class="some-class" ></asp:CheckBox>
$('.some-class').click(function () {
var checked = $(this).is(':checked');
if (checked) {
document.getElementById("confirm_value").value = "Yes";
if (!confirm('Are you sure you want to mark this order as received?')) {
$(this).removeAttr('checked');
}
}
else {
document.getElementById("confirm_value").value = "No";
if (!confirm('Are you sure you want to mark this order as not received?')) {
$(this).removeAttr('checked');
}
}
//return someVariableHoldTrueOrFalseForPostBack
//return true of false from here.
});
使用javascript
您可以在复选框上绑定javascript事件,它将自动应用于每行网格的所有生成的复选框。
<asp:CheckBox id="chkChoice" runat="server" OnClientClick="return yourFunction(this)" ></asp:CheckBox>
function yourFunction(source)
{
return confirm("your message");
}
要显示选中的弹出复选框
1.Create a div and design a popup as per your wish in your aspx page.
2.check if the checkbox is checked or not.
3.If it is checked then call the div as popup using its id.
c#代码:
if(checkedbox.checked==true)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Call my function", "showpopup();", true);
}
脚本:
function showpopup() {
$("#popup").fadeIn('slow');
}
其中#popup是您创建的popupdiv的id。
使用以下代码
$(document).ready(function() {
$("#gvOrders input:checkbox").click(function(e) {
if ($(this).is(":checked")) {
var m= confirm("your message");
if(m !=true)
{
$(this).removeAttr('checked');
e.preventDefault();
}
});
});