Monday, August 17, 2009

Ask For Confirmation Before Submitting Data in ASP.NET

Here's a simple way to ask the user for confirmation before submitting data:

C#

protected void Page_Load(object sender, EventArgs e)

{

string myScript = @"";

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MakeSure", myScript);

form1.Attributes.Add("onsubmit", "return askBeforeSubmit();");

}




VB.NET

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim myScript As String = ""

Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "MakeSure", myScript)

form1.Attributes.Add("onsubmit", "return askBeforeSubmit();")



End Sub




You can also ask for confirmation without writing any code. Just use the OnClientClick event on the button as shown below:


OnClientClick="return confirm('Are you sure?');"/>

No comments:

Post a Comment