Control.Dispose 方法

定義

啟用伺服器控制項,在它從記憶體釋放之前執行最後清除。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 物件,並在名為 myButtonButton 控制項上呼叫 Dispose 方法。The 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. 如果呼叫此版本的 Dispose 方法時擲回 Exception,控制項會將訊息寫入至目前的 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.

適用於

另請參閱