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控件关联的对象,并对名为myButtonButton控件调用方法。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的方法时引发,则该控件将向当前HttpResponse对象写入一条消息。 ExceptionIf 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.

适用于

另请参阅