Cancelling button Submission (programmatically)

The knowledge in this chapter applies for the usage within ASP.NET Web Form framework, not for ASP.NET MVC framework.

Think about a situation when you want that, if the BuyNow button is clicked, the Click Event handler method will check if a text box (i.e. a text box for email address) value is empty or not. If not empty then capture the value of the text box and proceed to paypal. If the textbox is empty, then show a text in a label "Please provide your email address" and then cancel the submission to PayPal. So, this time the user will fill up the text box and click the BuyNow Button again. Once it is clicked again, the method will check and find that the TextBox is not empty and this time it will proceed to PayPal. The following snippet shows how to do this.

protected void btnBuyNow_Click(object sender, ImageClickEventArgs e)
{
   if (txtEmailAddress.Text == String.Empty)
   {

      lblErrorMsg.Text = "Email addres is missing ! Please provide your email address.";

      btnBuyNow.CancelSubmission();

      return;
   }

   // proceed to PayPal.

}

CancelSubmission() is a public method available in all Payment Buttons (i.e. BuyNow button, Donation button, Subscription button, Upload Complete Cart button, AddToCart button etc).

This method is useful when you are handling "Click" / "Command" Event. When the button is clicked, the Click event handler will be executed before the page is being transferred to PayPal. Now, inside the Click Event handler method, if you want to conditionally stop transferring the page to PayPal then you can call this method. If you do not call this method, then as soon as the Click Event handler method is executed, the page will be transferred to PayPal.

Last updated on Nov 2, 2014