1

I have a grdiview in which I have added the Multi-delete functionality. Please see the code for your reference:-

<script type="text/javascript">
    function ValidateAll() {
        var chkselectcount = 0;
        var gridview = document.getElementById('<%= grdTeacherProfile.ClientID %>');
        for (var i = 0; i < gridview.getElementsByTagName("input").length; i++) {
            var node = gridview.getElementsByTagName("input")[i];

            if (node != null && node.type == "checkbox" && node.checked) {
                chkselectcount = chkselectcount + 1;
            }
        }
        if (chkselectcount == 0) {
            alert("Please select atleast One CheckBox");
            return false;
        }
        else {
            ConfirmationBox();
        }
    }
    function ConfirmationBox() {
        var result = confirm("Are you sure, you want to delete the Users ?");
        if (result) {
            return true;
        }
        else {
            return false;
        }
    }
</script>

Also see the button html:-

<asp:Button ID="btnDelete" runat="server" CausesValidation="false" CssClass="btn btn-danger" Text="Delete" OnClick="btnDelete_Click" OnClientClick="javascript:return ValidateAll();" />

The issue is that, when I check the checkboxes and Click on delete button, it asks for confirmation. When I click on Cancel it still deletes the row from the Gridview as well as sql table.

What should I do for the proper working of this. ? Please suggest

2
  • OnClick="btnDelete_Click" try removing it. Commented Dec 26, 2014 at 6:49
  • @Jai: Its fine, the answer given by Shekar is working.. Commented Dec 26, 2014 at 6:57

1 Answer 1

1

I think you need to use

return ConfirmationBox();

instead of

ConfirmationBox();

So your code becomes

function ValidateAll() {
    var chkselectcount = 0;
    var gridview = document.getElementById('<%= grdTeacherProfile.ClientID %>');
    for (var i = 0; i < gridview.getElementsByTagName("input").length; i++) {
        var node = gridview.getElementsByTagName("input")[i];

        if (node != null && node.type == "checkbox" && node.checked) {
            chkselectcount = chkselectcount + 1;
        }
    }
    if (chkselectcount == 0) {
        alert("Please select atleast One CheckBox");
        return false;
    }
    else {
        return ConfirmationBox();
    }
}

You need to remove the javascript: from OnClientClick you can use OnClientClick="return ValidateAll();"

Sign up to request clarification or add additional context in comments.

3 Comments

it is not working, it is still deleting the row. Did you checked my button html code ?
the button client click seems to be fine. though you can remove javascript: and write juse OnClientClick="return ValidateAll();"
yes, that is working now.. please update your answer so that I can mark it.. :) Thanks Shekhar.

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.