0

This is a Datalist in my code.

<asp:DataList runat="server" ID="dlstate" RepeatColumns="6">
     <ItemTemplate>
         <asp:CheckBox runat="server" ID="chk" Text='<%#Eval("area_state") %>' OnCheckedChanged="chk_state_SelectedIndexChanged" onchange="return checkconfirm(this);" AutoPostBack="true" />
     </ItemTemplate>
</asp:DataList>

This is the Javascript code.

<script type="text/javascript">
    function checkconfirm(a) {
        if (a.checked == true) {
            return true;
        }
        else {
            debugger;
            var box = confirm("Are you sure you want to unselect this state as areas alloted under this state will be lost?");
            if (box == true)
                return true;
            else {
                a.checked = true;
                return false;
            }
        }
    }
</script>

The problem is that when this apsx page is rendered in html the onchange="return checkconfirm(this);" appears on a span rather than the checkbox. I have tried several ways for confirmation and yet I am not able to put the onchange event on the checkbox.

1
  • try to change onchange with OnClick Commented May 11, 2017 at 6:56

1 Answer 1

1

You can use a jQuery listener for this.

<script type="text/javascript">
    $(document).ready(function () {
        $("#<%= dlstate.ClientID %> input[type='checkbox']").change(function () {
            if (!$(this).prop("checked")) {
                var box = confirm("Are you sure you want to unselect this state as areas alloted under this state will be lost?");
                if (box == true)
                    return true;
                else {
                    $(this).prop("checked", "checked");
                    return false;
                }
            }
        });
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.