Name

Init

Synopsis

Sub Page_Init(Sender As Object, e As EventArgs)
    ‘initialization code
End Sub

Parameters

Sender

An argument containing information about the object that raised the event.

e

An object of type EventArgs containing additional information about the event.

Example

The code example initializes a variable for setting the ForeColor property of a label in Page_Init, and then modifies that value to set the ForeColor property of another label in Page_Load:

<%@ Page Language="vb" %>
<html>
   <head>
      <title>Init event example</title>
      <script runat="server">
         Dim TheColor As System.Drawing.Color
         Sub Page_Init(  )
            TheColor = System.Drawing.Color.Red
         End Sub
         Sub Page_Load(  )
            Message.ForeColor = TheColor
            Message.Text = "The color of the text was set in Page_Init."
            TheColor = System.Drawing.Color.Blue
            Message2.ForeColor = TheColor
            Message2.Text = "The color of the text was set in Page_Load."
         End Sub
      </script>
   </head>
<body>
   <asp:label id="Message" runat="server"/>
   <br/>
   <asp:label id="Message2" runat="server"/>
</body>
</html>

Notes

Note that the Sender and e arguments are optional for this event, as shown in the example.

When the AutoEventWireup attribute of the @ Page directive is set to True (the default), ASP.NET will automatically call the event handler for this event, as long as it has the signature Page_Init.

Get ASP.NET in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.