In this article I will explain with an example, how to build a server side Yes/No confirmation box using JavaScript in ASP.Net using C# and VB.Net.
The JavaScript confirm function allows PostBack when OK is clicked but does nothing when Cancel is clicked and on many occasions we want to do Server-Side operations on both OK and Cancel click events in ASP.Net using C# and VB.Net.
 
 

HTML Markup

The following HTML Markup consists of:
Button – For displaying the confirmation box.
The Button has been assigned with an OnClick and JavaScript OnClientClick event handlers.
<asp:Button ID="btnConfirm" runat="server" Text="Raise Confirm" OnClientClick="Confirm()" OnClick="OnConfirm" />
 
 

Displaying Confirmation Box using JavaScript

When the Button is clicked, the Confirm JavaScript function is called.
Inside the Confirm JavaScript function, a dynamic HTML INPUT HiddenField element is created using JavaScript.
Then, JavaScript confirm function is called and its response (Yes or No) is set into the Hidden Field.
Finally, the HiddenField element is added to the Form and the PostBack is occurred which triggers the OnClick event.
<script type="text/javascript">
    function Confirm() {
        var confirm_value = document.createElement("INPUT");
        confirm_value.type = "hidden";
        confirm_value.name = "confirm_value";
        if (confirm("Do you want to save data?")) {
            confirm_value.value = "Yes";
        } else {
            confirm_value.value = "No";
        }
        document.forms[0].appendChild(confirm_value);
    }
</script>
 
 

Displaying Message based on the User input in Server-Side

When the Raise Confirm button is clicked, the value of the HiddenField is fetched using Request.Form collection.
Finally, based on the HiddenFiled value an appropriate message is displayed in JavaScript Alert Message Box using RegisterStartupScript method.
Note: For more details on how to use RegisterStartupScript method, please refer my article RegisterStartupScript: Call JavaScript function with parameters in ASP.Net using C# and VB.Net.
 
C#
protected void OnConfirm(object sender, EventArgs e)
{
    string confirmValue = Request.Form["confirm_value"];
    if (confirmValue == "Yes")
    {
        ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
    }
    else
    {
        ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);
    }
}
 
VB.Net
Protected Sub OnConfirm(sender As Object, e As EventArgs)
    Dim confirmValue As String = Request.Form("confirm_value")
    If confirmValue = "Yes"Then
        ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('You clicked YES!')", True)
    Else
        ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('You clicked NO!')", True)
    End If
End Sub
 
 

Screenshot

ASP.Net Server Side Yes No Confirmation Box using JavaScript
 
 

Browser Compatibility

The above code has been tested in the following browsers.
Microsoft Edge  FireFox  Chrome  Safari  Opera
* All browser logos displayed above are property of their respective owners.
 
 

Demo

 
 

Downloads