ASP.NET Page Events


ASP.NET developers consistently work with various events in their server-side code. Many of the events that they work with pertain to specific server controls. For instance, if you want to initiate some action when the end user clicks a button on your Web page, you create a button-click event in your server-side code, as shown in the following example:

  Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click    Label1.Text = TextBox1.Text End Sub 

In addition to the server controls, developers also want to initiate actions at specific moments when the ASP.NET page is being either created or destroyed. The ASP.NET page itself has always had a number of events for these instances. Following is a list of all the page events you could use in ASP.NET 1.0/1.1:

  • AbortTransaction

  • CommitTransaction

  • DataBinding

  • Disposed

  • Error

  • Init

  • Load

  • PreRender

  • Unload

One popular page event from this list is Load, which is used in VB as shown in the following (called Example 4 for later reference):

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)    Handles Me.Load    Response.Write("This is the Page_Load event") End Sub 

Besides the page events just shown, ASP.NET 2.0 adds the following new events:

  • InitComplete: Indicates that initialization of the page is completed

  • LoadComplete: Indicates that the page has been completely loaded into memory

  • PreInit: Indicates the moment immediately before a page is initialized

  • PreLoad: Indicates the moment before a page has been loaded into memory

  • PreRenderComplete: Indicates the moment directly before a page has been rendered in the browser

You construct these new page events just as you did the previously shown page events. For example, you use the PreInit event as follows:

  <script runat="server" language="vb">     Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs)        Page.Theme = Request.QueryString("ThemeChange")     End Sub </script> 

If you create an ASP.NET 2.0 page and turn on tracing, you can see the order in which the main page events are initiated:

  1. PreInit

  2. Init

  3. InitComplete

  4. PreLoad

  5. Load

  6. LoadComplete

  7. PreRender

  8. PreRenderComplete

  9. Unload

With the addition of these new options, you can now work with the page and the controls on the page at many different points in the page-compilation process.




Professional VB 2005 with. NET 3. 0
Professional VB 2005 with .NET 3.0 (Programmer to Programmer)
ISBN: 0470124709
EAN: 2147483647
Year: 2004
Pages: 267

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net