What the page load event is for
Every time a page runs, the server raises a load event. It is the natural home for work that must happen whenever the page appears.
Typical uses
- Filling a dropdown list with choices.
- Showing a default value on first visit.
- Reading a value from a database to display.
The trap to avoid
Load runs again on every postback, not only the first visit. If you add items to a list here without checking, the list can end up with duplicates after the visitor clicks a button. Check whether this is the first load before you fill it.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
sizeList.Items.Add("Small");
sizeList.Items.Add("Large");
}
}First load only. Use the postback check for anything that should happen once. Leave it out for work that must run on every request, such as reading the current visitor name.
A related event fires when a specific control changes, covered in reacting to a change in a text box.