简体   繁体   中英

GridView Delete with bootbox.confirm always returning false

I'm trying to delete a record from a Gridview using a LinkButton inside a TemplateField. My LinkButton should open a bootbox.confirm() and based on the return value do a call to the GridView.RowCommand handler in the backend.

GridView Layout:

<asp:GridView ID="gvEntRelationship" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"  DataKeyNames="OFTID" DataSourceID="ObjectDataEntRelations" EnableModelValidation="True" >`
   <Columns>
     <asp:CommandField ShowEditButton="True" />
     <asp:TemplateField ShowHeader="False">
        <ItemTemplate>
           <asp:LinkButton ID="lnkDelRel" runat="server" CausesValidation="False" CommandName="Del" Text="Delete" OnClientClick="return OpenDeleteBox(this);"></asp:LinkButton>
        </ItemTemplate>
     </asp:TemplateField>
  </Columns>
</asp:GridView>

Javascript:

function OpenDeleteBox(sender) {
            bootbox.confirm("Delete This?", function (confirmed) {
                if (confirmed) {
                    alert('if');
                    return true;
                }
            });
            alert('outside');
            return false;         
        }

I'm using the OnClientClick to call the javascript function. When inside function, I see the alert('outside') (implying it already returns false) and then the bootbox shows up. If I move the return false into an else clause like:

 bootbox.confirm("Delete This?", function (confirmed) {
                if (confirmed) {
                    alert('in if');
                    return true;
                }
                else {
                    return false;
                }
            });

It goes ahead and deletes the record using the GridView.RowCommand handler without waiting for the confirmbox to even show up.

EDIT: seemed to do the job

function OpenDeleteBox(sender) {
            if ($(sender).attr("confirmed") == "true") { return true; }
            bootbox.confirm("Delete This?", function (confirmed) {
                if (confirmed) {
                    $(sender).attr("confirmed", confirmed);
                    sender.click();
                }
            });

            return false;

        }

Check out these posts.

OnClientClick="return confirm('Are you sure you want delete');"

Confirm for Linkbutton before performing delete in asp.net

I think you need to use default javascript confirm function with LinkButton. Seems bootbox confirm cannot be used. It's the default behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM