Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

Control.OnLoad Method

Raises the Load event.

[Visual Basic]
Protected Overridable Sub OnLoad( _
   ByVal e As EventArgs _
)
[C#]
protected virtual void OnLoad(
 EventArgs e
);
[C++]
protected: virtual void OnLoad(
 EventArgs* e
);
[JScript]
protected function OnLoad(
   e : EventArgs
);

Parameters

e
The EventArgs object that contains the event data.

Remarks

This method notifies the server control that it should perform actions common to each HTTP request for the page it is associated with, such as setting up a database query. At this stage in the page lifecycle, server controls in the hierarchy are created and initialized, view state is restored, and form controls reflect client-side data.

Example

[Visual Basic, C#] The following example creates a text stream object, myFile, and uses it to print messages to a text file when defined Page and control lifecycle events occur during request processing. During the OnLoad method call, the string "Custom control has loaded" is written to the file and the text stream object is closed.

[Visual Basic] 
' Create a StreamWriter to write data to a text file.
 Dim myFile As TextWriter = File.CreateText("c:\NewTextFile.txt")

Sub Page_Load(sender As Object, e As EventArgs)
   ' Write status to file.
   myFile.WriteLine("Page has loaded.")
End Sub

Sub CustomControl_OnLoad(sender As Object, e As EventArgs)
   myFile.WriteLine("Custom control has loaded.")
End Sub

Sub CustomControl_OnUnload(sender As Object, e As EventArgs)
   ' Server controls final cleanup such as;
   ' closing files etc.goes here         
   myFile.WriteLine("Custom control was unloaded.")
   ' Close the stream object.
   myFile.Close()
End Sub

[C#] 
// Create a StreamWriter to write data to a text file.
TextWriter myFile = File.CreateText("c:\\NewTextFile.txt");

void Page_Load(object sender,EventArgs e)
{
   // Write status to file.
   myFile.WriteLine("Page has loaded.");
}
void CustomControl_OnLoad(object sender,EventArgs e)
{
   myFile.WriteLine("Custom control has loaded.");
}

void CustomControl_OnUnload(object sender,EventArgs e)
{
   // Server controls final cleanup such as; 
   // closing files goes here         
   myFile.WriteLine("Custom control was unloaded.");
  // Close the stream object.
   myFile.Close();
}

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic 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 | Page

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.