Thursday, April 18, 2024

Confirming ASP.NET Buttons With Bootstrap And Bootbox!

I have been doing a lot of work in ASP.NET with Bootstrap. One challenge has been getting ASP.NET server controls to behave nicely within the Bootstrap scaffolding. I needed an elegant way to confirm a postback action wired to an ASP.NET button control. 

One can easily leverage the Javascript confirm function along with the OnClientClick property of the button controls.  

<asp:Button ID="Button1" runat="server" Text="Delete" 
OnClientClick="return confirm('Are you sure?');"></asp:LinkButton>

However, I am using Bootstrap.  I wanted a fancy Bootstrap dialog box to appear for such a prompt instead of the generic-looking alert box.  This is what I came up with.

<script type="text/javascript">
    var confirmed = false;
    function Confirm(btn) {
        if (confirmed) return true;
        bootbox.confirm("Are you sure?", function (result) {
            if (result) {
                confirmed = true;
                btn.click();
            }
        });
        return false;
    }
</script>

<asp:Button runat="server" ID="Button1" Text="Update" CssClass="btn btn-warning" 
OnClick="UpdateButton_OnClick" OnClientClick="return Confirm(this);" />
The code requires not only Bootstrap, but also Bootbox.js.  In a nutshell, Bootbox is a library that simplifies the creation of Bootstrap dialog boxes.  You should check it out!

There is a global boolean variable named "confirmed" that starts its life out as false. When the button if clicked, the "confirmed" variable is evaluated, found to be false, and a Bootbox confirm dialog is displayed. The event function returns a false to prevent the button action from firing. If the Bootbox confirm dialog that appears returns a true response, meaning the user approves the action, the confirmed variable is changed to true and the button click event is fired once again.

This time, however, the "confirmed" variable is found to be true, so the event is returned as "true" allowing the button to do its thing.

How I Upgraded (Hacked) The SASS Compiler for Mads Christensen's WebCompiler

Mads Christensen's Web Compiler has not been updated since 2015. However, I discovered a way to freshen the SASS compiler to the most recent version of node-sass available. Here are my steps...

Use Void Tools' Everything to find the WebCompiler temp directory by searching for WebCompiler.exe. Mine was located in "C:\Users\[user_name]\AppData\Local\Temp\WebCompiler1.12.394". By the way, if you are not already using Everything to search for files, do yourself a favor and install it!

Run “npm install node-sass” in WebCompiler temp directory. This will upgrade the SASS compiler to the latest version.

Next you will need to upgrade the node.exe file that is found in the WebCompiler temp directory. I do not know the "proper" way to accomplish this; but here is what I did...

  1. Use Everything to find latest version of node.exe (v20.12.0)
  2. Copy the latest version to the WebCompiler temp directory
That should do it!