Submitting the button by Server Side Method Call

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

There are times when you may want to use your own custom control or standard ASP.NET server side controls (i.e. Button, ImageButton, CheckBox, TextBox etc) for triggering BuyNow (or other button types like Donation, Add to Cart etc) button. In that situation, you can simply call the public method Submit() of this control within your server side methods. You can call this Submit() method even when your PayPal button is Disabled (Enabled = false) or Invisible (Visible = false). Calling Submit() method from ASP.NET AJAX Update Panel may not work.Problem:Imagine a situation where you need to submit BuyNow Button as soon as a radio button is checked. The radio button is used to determine the payment method offered to the customer. For example, you offered payment methods "PayPal", "Credit Card", "2CheckOut" etc as follows: PayPal Credit Card 2CheckOutSolution:

  1. Place an instance of ASP.NET PayPal Control and set ID = "btnBuyNow". You may choose not to show this button as your customer may get confused when he/she will see check box and button together. So, set btnBuyNow.Visible = false.
  2. Set all properties of the PayPal Control according to your business purposes.
  3. Place 3 RadioButtons as shown above and set AutoPostBack = True for these RadioButtons.
  4. Attach CheckedChanged event handler for these radio buttons and in your CheckedChanged event handler, check if the Checked RadioButton was associated with "PayPal" option and if so, call btnBuyNow.Submit() method. Please follow the following snippet.

    26 protected void rdoPayWithPayPal_CheckedChanged(object sender, EventArgs e)

    27 {

    28 if (rdoPayWithPayPal.Checked)

    29 {

    30 btnBuyNow.Amount = 12.34m;

    31 btnBuyNow.ItemName = "MyDigitalImage";

    32 btnBuyNow.Submit();

    33 }

    34 else

    35 {

    36 // Other payment methods.

    37 }

    38 }

  5. Therefore, whenever PayPal radio control is checked, BuyNow button will be submitted. If any Click Event handler

    like "protected void btnBuyNow_Click(object sender, ImageClickEventArgs e)"

    is attached to "btnBuyNow", "btnBuyNow_Click" method will not be executed as it will cause double postbacks. In order to execute codes before being transferred to PayPal website, you can include your code in the Submit() caller method as shown in the above snippet (rdoPayWithPayPal_CheckedChanged).
Last updated on Nov 1, 2014