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.
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
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>
<asp:Button ID="btnDetails" runat="server" Text="GetValue" OnClick="btnDetails_Click" />
<asp:Button ID="btnAlelrt" runat="server" Text="GetDetails" OnClick="btnAlelrt_Click" />
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
//hide the button from users visibility
protected void btnDetails_Click(object sender, EventArgs e)
//Write your code which you wish to execute before the Confirmation from the USerthis.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);
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();.
4. This ShowConfirmation will show the confirm to user and depend on the user input It will react.
So, while running the above code, we can get the confirmation from users between the postback.
By Vasanthakumar D Popularity (17956 Views)