Control.Dispose Control.Dispose Control.Dispose Control.Dispose Method

定義

啟用伺服器控制項,在它從記憶體釋放之前執行最後清除。Enables a server control to perform final clean up before it is released from memory.

public:
 virtual void Dispose();
public virtual void Dispose ();
abstract member Dispose : unit -> unit
override this.Dispose : unit -> unit
Public Overridable Sub Dispose ()

實作

範例

下列程式碼會覆寫Dispose方法以關閉HtmlTextWriter控制項,並呼叫相關聯的物件Dispose方法Button控制項,名為myButtonThe following code example overrides the Dispose method to close the HtmlTextWriter object associated with a control, and call the Dispose method on a Button control, named myButton. 如果Exception時擲回這個版本的Dispose呼叫方法時,控制項將訊息寫入至目前HttpResponse物件。If an Exception is thrown when this version of the Dispose method is called, the control writes a message to the current HttpResponse object.

public override void Dispose()
{
   try
   {
      Context.Response.Write("Disposing " + ToString());
      // Perform resource cleanup.
      myTextWriter.Close();
      myButton.Dispose();
   }
   catch(Exception myException)
   {
      Context.Response.Write("Exception occurred: "+myException.Message);
   }
}
Public Overrides Sub Dispose()
   Try
      Context.Response.Write("Disposing " & ToString())
      ' Perform resource cleanup.
      myTextWriter.Close()
      myButton.Dispose()
   Catch myException As Exception
      Context.Response.Write("Exception occurred: " & myException.Message)
   End Try
End Sub

備註

Dispose 使用完畢時,請呼叫 ControlCall Dispose when you are finished using the Control. Dispose 方法會將 Control 保留在無法使用的狀態。The Dispose method leaves the Control in an unusable state. 之後呼叫這個方法,您必須釋放控制項的所有參考,讓它所佔用的記憶體可以回收記憶體回收。After calling this method, you must release all references to the control so the memory it was occupying can be reclaimed by garbage collection.

適用於

另請參閱