TemplateControl.AbortTransaction 事件

定義

在使用者結束交易時發生。

C#
public event EventHandler AbortTransaction;

事件類型

範例

下列程式代碼範例示範如何註冊 AbortTransaction 事件的自定義事件處理程式。 因為 TemplateControl 是抽象類,所以這個程式代碼範例會使用 Page 衍生自 類別的 TemplateControl 類別。 Account 是支援 Debit 作業的類別,其為交易式,而且必須以單位執行。 如果在此作業期間引發任何例外狀況,交易就會結束。

C#

  private void Page_Load(object sender, System.EventArgs e)
  {
      AbortTransaction += new System.EventHandler(Sub_AbortTransaction);
   CommitTransaction += new System.EventHandler(Sub_CommitTransaction);
      try
   {
      Account myAccount = new Account();
      int someAmount = 500;
      myAccount.Debit(someAmount); 
      ContextUtil.SetComplete(); 
   }
   catch(Exception)
   {
      ContextUtil.SetAbort();
   }
  }

private void Sub_AbortTransaction(object sender,System.EventArgs e)
{
   // Code for RollBack activity goes here.
   Response.Write("Transaction Aborted");
}
private void Sub_CommitTransaction(object sender,System.EventArgs e)
{
   // Code for Commit Activity goes here.
   Response.Write("Transaction Commited");
}

適用於

產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

另請參閱