In this article, I will explain how you can display a confirmation dialog from server side in ASP.NET. Suppose you are performing an operation and wish to present the user with a confirmation dialog. Using this code, the user will be shown a confirmation dialog box with "Yes" and "No" buttons. Then on the server side, you can test which button was clicked and take action accordingly.

Default.aspx






    
        function DisplayConfirmationMessage(arg1, arg2) {
            if (confirm("The 1st argument is \"" + arg1 + "\" and the 2nd argument is \"" + arg2 + "\". Do you really want to proceed?")) {
                __doPostBack('', "true");
                return false;
            }
            return false;
        }
    


    
    <div>
        
        
    </div>
    


Default.aspx.cs

using System;
using System.Web.UI;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected override void Render(HtmlTextWriter writer)
    {
        try
        {
            ClientScript.RegisterForEventValidation(linkbtnPerformAction.UniqueID, "true");
            base.Render(writer);
        }
        catch
        {
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            string strArgument1 = "Varun";
            string strArgument2 = "Sood";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "DisplayConfirmationMessage('" + strArgument1 + "', '" + strArgument2 + "');", true);
        }
        catch
        {
        }
    }

    protected void linkbtnPerformAction_Click(object sender, EventArgs e)
    {
        try
        {
            string strConfirmationResult = (this.Request["__EVENTARGUMENT"] == null) ? string.Empty : this.Request["__EVENTARGUMENT"];
            if (strConfirmationResult.Equals("true"))
            {
                Response.Redirect("http://www.google.com", false);
            }
        }
        catch
        {
        }
    }
}

This code is fully tested. If you still face any trouble using this code, feel free to get in touch with me through the comments section below or the contact page.

  • vinod says:

    what is linkbtnPerformAction control here

    • Varun Sood says:

      Hi Vinod, I apologize for the delayed reply. I was recovering from COVID.

      linkbtnPerformAction is the ID of the hidden link button that you can find in the code of the Default.aspx page above.

      Thanks!