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

Control.OnLoad Method

Raises the Load event.

Namespace: System.Web.UI
Assembly: System.Web (in system.web.dll)
protected internal virtual void OnLoad (
	EventArgs e
)
protected void OnLoad (
	EventArgs e
)
protected internal function OnLoad (
	e : EventArgs
)
Not applicable.

Parameters

e

The EventArgs object that contains the event data.

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.

Use the IsPostBack property to determine whether the page is being loaded in response to a client postback, or if it is being loaded and accessed for the first time.

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.

// 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();
}

// 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.");
} //Page_Load

void CustomControl_OnLoad(Object sender,EventArgs e)
{
    myFile.WriteLine("Custom control has loaded.");
} //CustomControl_OnLoad

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();
} //CustomControl_OnUnload

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.