I am often asked how to intercept the postback when an <asp:Button/> is
clicked. The answer is not immediately obvious but once you know the
trick it is very straight forward.
What makes this tricky is that an <asp:Button/> renders as <input
type="submit" ... /> which means that the page will submit automatically
when the button is clicked.
This appears to not give you an opportunity to intercept the click however
the first little bit of magic you need to know is that if the button has an
"onclick" event that will fire before the page is submitted and the
second little bit of magic to know is that the boolean return value from your
"onclick" event determines if the page submission continues (return
true) or not (return false).
So, this sample code will ask you to confirm the postback and provide you and
option to cancel the submittion:
WebForm1.aspx
<%@ Page language="c#"
Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="ButtonClick.WebForm1" %>
<!DOCTYPE
HTML
PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN"
>
<HTML>
<HEAD>
<title>WebForm1</title>
<meta
name="GENERATOR"
Content="Microsoft
Visual Studio .NET 7.1">
<meta
name="CODE_LANGUAGE"
Content="C#">
<meta
name="vs_defaultClientScript"
content="JavaScript">
<meta
name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form
id="Form1"
method="post"
runat="server">
<P>Click
the button to trigger the confirmButton() javascript function.</P>
<P><asp:Button
id="ButtonSubmit"
runat="server"
Text="Submit"></asp:Button></P>
<P><asp:Label
id="LabelStatus"
runat="server">Status:
</asp:Label></P>
</form>
</body>
</HTML>
WebForm1.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace ButtonClick
{
public
class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button ButtonSubmit;
protected System.Web.UI.WebControls.Label LabelStatus;
private void Page_Load(object
sender, System.EventArgs e)
{
string jsConfirm = @"<script language='javascript'>
function confirmButton()
{
var agree=confirm('Are you sure you wish to continue?');
if (agree)
return true;
else
return false;
}
</script>";
Page.RegisterClientScriptBlock("confirm",jsConfirm);
ButtonSubmit.Attributes.Add("onclick","return confirmButton();");
}
#region Web Form Designer generated code
override protected
void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support -
do not modify
/// the contents of this method with the
code editor.
/// </summary>
private void InitializeComponent()
{
this.ButtonSubmit.Click += new System.EventHandler(this.ButtonSubmit_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void ButtonSubmit_Click(object
sender, System.EventArgs e)
{
LabelStatus.Text = "Status: ASP Button (Server click)";
}
}
}
Easy when you know how!
Posted
Wed, Nov 24 2004 9:16
by
jonathangreensted