Thursday, October 2, 2008

Confirm Box in Asp.Net

/***********************************************************************************************************************/
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ConfirmBoxTest.aspx.cs" Inherits="ConfirmBoxTest" %>
<!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 id="Head1" runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function confirm_delete()
{
if (confirm("This record will be deleted?")==true)
return true;
else
return false;
}
</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnDelete" runat="server" Height="53px" Text="Delete"
Width="198px" OnClick="btnDelete_Click" />

<asp:Button ID="btnUpdate" runat="server" Height="53px" Text="Update"
Width="198px" OnClick="btnUpdate_Click" /><br>
<asp:TextBox ID="Name" runat="server"></asp:TextBox></div>
</form>
</body>
</html>
/***********************************************************************************************************************/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 ConfirmBoxTest : System.Web.UI.Page
{
private string scriptKey = "CONFIRM_TEST";
protected void Page_Load(object sender, EventArgs e)
{
// to Insure that the __doPostBack() JavaScript is added to the page...
ClientScript.GetPostBackEventReference(this, string.Empty);
//this.GetPostBackEventReference(this, string.Empty);

if ( this.IsPostBack )
{
//to perform Update button events
ConfirmBoxChecking();
}

if (!IsPostBack) btnDelete.Attributes.Add("onclick", "return confirm_delete();");
}
protected void btnDelete_Click(object sender, EventArgs e)
{
Response.Write("I am in Delete button");
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
bool isConfirmNeeded = true;
string confirmMessage = string.Empty;
Response.Write("I am in update button");
if (isConfirmNeeded)
{
confirmMessage = "Do you want to really update the value?";
System.Text.StringBuilder javaScript = new System.Text.StringBuilder();
javaScript.Append("\n<script type=text/javascript>\n");
javaScript.Append("var userConfirmation = window.confirm('" + confirmMessage + "');\n");
javaScript.Append("__doPostBack('UserConfirmationPostBack', userConfirmation);\n");
javaScript.Append("</script>\n");
//RegisterStartupScript(scriptKey, javaScript.ToString());
ClientScript.RegisterStartupScript(this.GetType(), scriptKey, javaScript.ToString());
}
}

private void ConfirmBoxChecking()
{
string eventTarget = (this.Request["__EVENTTARGET"] == null) ? string.Empty : this.Request["__EVENTTARGET"];
if (eventTarget == "UserConfirmationPostBack")
{
string eventArgument = (this.Request["__EVENTARGUMENT"] == null)? string.Empty : this.Request["__EVENTARGUMENT"];
if (eventArgument == "true")
{
Response.Write("Selected Value is TRUE");
Response.Write("<BR>" + Name.Text);
//Do all True events here
}
else
{
Response.Write("Selected Value is FALSE");
Response.Write("<BR>" + Name.Text);
//Do all cancel events here
}
}
}
}
/***********************************************************************************************************************/

No comments: