In this article I will explain with an example, how to implement confirm before
Submit on
ASP.Net Button Click using
JavaScript.
The
JavaScript Confirmation Message Box will be displayed on
Button Click using
ClientScript.RegisterOnSubmitStatement function.
When the
Button is clicked, the Confirmation Message Box with OK (Yes) and Cancel (No)
buttons will be displayed, if the User clicks OK (Yes) button only then the Form will be submitted i.e. Click event will fire.
HTML Markup
The following
HTML Markup consists of:
TextBox - For capturing user input.
Button - For submitting user input.
The
Button have been assigned with an
OnClick event handler.
<form id="form1" runat="server">
<table>
<tr>
<td>Name:</td>
<td><asp:TextBox ID="txtName" runat="server" /></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="Submit" /></td>
</tr>
</table>
</form>
Displaying JavaScript Confirmation Box Before Submit on ASP.Net Button Click
Inside the
Page Load event, the script to display
JavaScript Confirmation Message Box is registered using the
ClientScript.RegisterOnSubmitStatement function.
C#
protected void Page_Load(object sender, EventArgs e)
{
string message = "Do you want to submit?";
ClientScript.RegisterOnSubmitStatement(this.GetType(), "confirm", "return confirm('" + message + "');");
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim message As String = "Do you want to submit?"
ClientScript.RegisterOnSubmitStatement(Me.GetType(), "confirm", "return confirm('" & message & "');")
End Sub
The Button Click event
Inside the
Button Click event, the script to display
JavaScript Alert Message Box is registered using the
ClientScript.RegisterStartupScript function.
C#
protected void Submit(object sender,EventArgs e)
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Form submitted.');", true);
}
VB.Net
Protected Sub Submit(ByVal sender As Object, ByVal e As EventArgs)
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Form submitted.');", True)
End Sub
Screenshot
Browser Compatibility
* All browser logos displayed above are property of their respective owners.
Demo
Downloads