Display a Confirmation Dialog from Server Side in ASP.NET

0
1822
Display Confirmation Dialog from Server Side in ASP.NET

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

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script type="text/javascript">
        function DisplayConfirmationMessage(arg1, arg2) {
            if (confirm("The 1st argument is \"" + arg1 + "\" and the 2nd argument is \"" + arg2 + "\". Do you really want to proceed?")) {
                __doPostBack('<%= this.linkbtnPerformAction.UniqueID %>', "true");
                return false;
            }
            return false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
        <asp:LinkButton ID="linkbtnPerformAction" runat="server" OnClick="linkbtnPerformAction_Click"
            Style="display: none;"></asp:LinkButton>
    </div>
    </form>
</body>
</html>

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.

LEAVE A REPLY

Please enter your comment
Please enter your name