当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Popup window when checkbox checked in asp.net

问题描述:

I would like to show a confirmation Box with the message when the user check the CheckBox.

So i have a GridView and there is a column for CheckBoxes.

So whenever user check the CheckBox i would like to show a confirmation box and when user click cancel on that box i would like to uncheck that CheckBox.

when user press OK then i would like to fire a standard asp.net CheckBox_CheckedChanged where i am doing some database work.

I dont know how to do that in javascript or Jquery.

I found it on google where there is only one CheckBox and you can use the ID and using the Jquery you can show the popup.
BUT i have a GridView and there are lots of CheckBoxes for each row.

Please suggest me some working example or Code.

Thanks

***** EDITS *******

here is the code i have got so far.

$('#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');

}

});

This is not working so far when CheckBox checked. I am not sure what i am doing wrong here.

*** HTML FOR GRIDVIEW *******

 <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") %>&nbsp;<%# 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>

网友答案:

Using jQuery

Assign some class to your gridview checkbox and bind the event on that class.

<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.
});

Using javascript

You can bind the javascript event on check box and it will be applied to all generated checkboxes of each row of grid automatically.

<asp:CheckBox id="chkChoice" runat="server" OnClientClick="return yourFunction(this)" ></asp:CheckBox>

function yourFunction(source)
{
     return confirm("your message"); 
}
网友答案:

To show a pop on checkbox checked

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# Code:

if(checkedbox.checked==true)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Call my function", "showpopup();", true); 
}

Script:

  function showpopup() {
    $("#popup").fadeIn('slow');
    }

where #popup is the id of the popup div which u have created.

网友答案:

use the following code

$(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(); 
           }     
       });
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: