User confirmation from code behind

Sometimes we need to get confirmation from the users between the execution of page. Consider the situatuion, I have customer details, while delete the customer, I need to check for any orders made by him. If orders exist, I need to get a confirmation from the users to delete the details. How do we achieve this? If we use the RegisterStartupScript, we can able to show the confirmation, but it will show after the page is rendered. So, no use even the confirmation is asked at client.


1.  you need to seperate the code into two pieces as "Before the User conformation" and "After the User confirmation". Suppose I want to delete the customer details as mentioned previously, Code for cheking the oreder details and shwoing confirmation from the code behind is Before User confirmation. And  code to delete the user details is Code After user confirmation.

2. Write the code before confirmation in your real delete button click event. Reagrding previous example, we need to write the code for getting the order details will be written in delete button click. And we need to call a Javascript confirmation from here.

3. Write the code after user confirmation in the button click event of a hdden button. Reagrding previous example, we beed to wirte the code for deleting the customer details will be written here.

Below is the sample aspx and CS code for demonstrae this... In this expample I will try to alert the textbox text from Code behind. Before show the alert I need to get the confirmation from user that whether I need to show alert or not.


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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" >

<head runat="server">

<title>Untitled Page</title>

<script type="text/javascript" language="javascript">

function ShowConfirmation()


         if(confirm("Are you want to show the value?")== true)


         //Calling the server side code after confirmation from the user






<form id="form1" runat="server">


<asp:TextBox ID="txtDetails" runat="server"></asp:TextBox>

<br />

<asp:Button ID="btnDetails" runat="server" Text="GetValue" OnClick="btnDetails_Click" />

<asp:Button ID="btnAlelrt" runat="server" Text="GetDetails" OnClick="btnAlelrt_Click" />

<br />

<br />





C# Code

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page


protected void Page_Load(object sender, EventArgs e)


//hide the button from users visibility

btnAlelrt.Style.Add("display", "none");


protected void btnDetails_Click(object sender, EventArgs e)


//Write your code which you wish to execute before the Confirmation from the USer
this.GetType(), "showAl", "ShowConfirmation();", true);


protected void btnAlelrt_Click(object sender, EventArgs e)


//write your code which you wish to execute after the confirmation from the user....
this.GetType(), "showVal", "alert('" + txtDetails.Text + "');", true);



Code Explanation:

1. In aspx I have added button btnAlert whihc is invisble. This is button is used for executing the code after user confirmation.
2. In GetValue button I have Used Register Startup script to call the Javascrpt function ShowConfirmation();.
3. The GetValue button is used for executing the code  before user Confirmation. While clicking this button It will call the Showconfirmation() javascript.
4. This ShowConfirmation will show the confirm to user and depend on the user input It will react.
5. If the user selects Ok, the hidden button which executes the code after user confirmatin, click event is fired from Javascript.

So, while running the above code, we can get the confirmation from users between the postback.

By Vasanthakumar D   Popularity  (16040 Views)