Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Control.Dispose Method

Enables a server control to perform final clean up before it is released from memory.

[Visual Basic]
Public Overridable Sub Dispose() Implements IDisposable.Dispose
[C#]
public virtual void Dispose();
[C++]
public: virtual void Dispose();
[JScript]
public function Dispose();

Implements

IDisposable.Dispose

Remarks

Call Dispose when you are finished using the 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.

Note   Always call Dispose before you release your last reference to the Control. Otherwise, the resources the Control is using will not be freed until garbage collection calls the Control object's destructor.

For more information on methods that are commonly overridden when creating custom server controls, see Methods in ASP.NET Server Controls.

Example

[Visual Basic, C#, C++] The following code example overrides the Dispose method to close the HtmlTextWriter boject associated with a control, and call the Dispose method on a Button control, named myButton. If an Exception is thrown when this version of the Dispose method is called, the control writes a message to the current System.Web.HttpRepsonse object.

[Visual Basic] 
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

[C#] 
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);
   }
}

[C++] 
public:
    void Dispose()
    {
        try
        {
            Context->Response->Write(String::Concat(S"Disposing ", ToString()));
            // Perform resource cleanup.
            myTextWriter->Close();
            myButton->Dispose();
        }
        catch(Exception* myException)
        {
            Context->Response->Write(String::Concat(S"Exception occurred: ", myException->Message));
        }
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

Control Class | Control Members | System.Web.UI Namespace | Control Execution Lifecycle | Methods in ASP.NET Server Controls

Show:
© 2015 Microsoft