最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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 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>

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 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>
Share Improve this question edited Feb 4, 2013 at 10:23 patel.milanb asked Feb 4, 2013 at 10:16 patel.milanbpatel.milanb 5,99215 gold badges61 silver badges95 bronze badges 3
  • put your html of your gridview – शेखर Commented Feb 4, 2013 at 10:21
  • see my edits. I have put my code but its not working – patel.milanb Commented Feb 4, 2013 at 10:24
  • gvOrders is your grid-view id? – शेखर Commented Feb 4, 2013 at 10:40
Add a ment  | 

3 Answers 3

Reset to default 3

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(); 
           }     
       });
});
发布评论

评论列表(0)

  1. 暂无评论