by Fred Mastro
3. July 2009 20:09
Good article I found on every event and the order they fire on a page. This is not my article, but I can't remember for the life of me where I got it. I had it saved into a txt file.
ASP.NET 2.0 Page Event Life Cycle
In this article, we will explore the various events that occur when an asp.net 2.0 page is requested from the server. Before exploring that, let us briefly take a look at the various components that participate in the request and response model for asp.net pages.
When a request is made to the server using HTTP (GET), IIS receives the request and passes it to the asp.net engine(dll) that compiles the page that is requested. The request passes through two sections :
Http Module – Monitors the request and takes action. Authentication and Authorization take place over here.
Http Handler – Responsible for compiling the page and generating output using System.Web.UI.PageHandlerFactory (we are considering .aspx page for this article).
Once compilation is done, the page is sent back to the Http module which then sends it to the IIS and back to the browser.
The events occur in the following sequence. Its best to turn on tracing(<% @Page Trace=”true”%>) and track the flow of events :
- PreInit – This event represents the entry point of the page life cycle. If you need to change the Master page or theme programmatically, then this would be the event to do so.
- Dynamic controls are created in this event.
- Init – Each control in the control collection is initialized.
- Init Complete* - Page is initialized and the process is completed.
- PreLoad* - This event is called before the loading of the page is completed.
- Load – This event is raised for the Page and then all child controls. The controls properties and view state can be accessed at this stage. This event indicates that the controls have been fully loaded.
- LoadComplete* - This event signals indicates that the page has been loaded in the memory. It also marks the beginning of the rendering stage.
- PreRender – If you need to make any final updates to the contents of the controls or the page, then use this event. It first fires for the page and then for all the controls.
- PreRenderComplete* - Is called to explicitly state that the PreRender phase is completed.
- SaveStateComplete* - In this event, the current state of the control is completely saved to the ViewState.
- Unload – This event is typically used for closing files and database connections. At times, it is also used for logging some wrap-up tasks.
There's a MasterPage, a Page, a UserControl, a Nested UserControl and the button clicked in the Nested User Control does a databind.
Our test setup is composed of a Page, MasterPage, UserControl, Nested UserControl and Button control as follows:
* The Page is tied to the MasterPage
* The UserControl is on the Page
* The Nested UserControl is on the UserControl
* The Button is on the Nested UserControl.
* Clicking the Button calls the Page.DataBind method
Each event on these controls has been set to call Debug.WriteLine as each event is raised. In addition to the events that get raised for regular pages, I've also set up an HttpModule and wired up all of those events as well. The results in the Debug output window of running this page and Clicking the Button are as follows:
BeginRequest - HttpModule
AuthenticateRequest - HttpModule
PostAuthenticateRequest - HttpModule
PostAuthorizeRequest - HttpModule
ResolveRequestCache - HttpModule
PostResolveRequestCache - HttpModule
PostMapRequestHandler - HttpModule
AcquireRequestState - HttpModule
PostAcquireRequestState - HttpModule
PreRequestHandlerExecute - HttpModule
PreInit - Page
Init - ChildUserControl
Init - UserControl
Init - MasterPage
Init - Page
InitComplete - Page
LoadPageStateFromPersistenceMedium - Page
ProcessPostData (first try) - Page
PreLoad - Page
Load - Page
Load - MasterPage
Load - UserControl
Load - ChildUserControl
ProcessPostData (second try) - Page
RaiseChangedEvents - Page
RaisePostBackEvent - Page
Click - Button - ChildUserControl
DataBinding - Page
DataBinding - MasterPage
DataBinding - UserControl
DataBinding - ChildUserControl
LoadComplete - Page
PreRender - Page
PreRender - MasterPage
PreRender - UserControl
PreRender - ChildUserControl
PreRenderComplete - Page
SaveViewState - Page
SavePageStateToPersistenceMedium - Page
SaveStateComplete - Page
Unload - ChildUserControl
Unload - UserControl
Unload - MasterPage
Unload - Page
PostRequestHandlerExecute - HttpModule
ReleaseRequestState - HttpModule
PostReleaseRequestState - HttpModule
UpdateRequestCache - HttpModule
PostUpdateRequestCache - HttpModule
EndRequest - HttpModule
PreSendRequestHeaders - HttpModule
PreSendRequestContent - HttpModule
by Fred Mastro
29. June 2009 09:45
So here's a personal post. I had an interesting weekend it was good and bad. To sum it up... I got tipsy, had my car towed while I was sleeping to wake up with no idea where my car went. Cost me $200. Got my fingers caught in the car door as someone closed it on me, took out two russian girls out around tampa (which then one lost their wallet and we had to back track until we found it), released a web application project into production that I've been working on for the last 3 months, worked out twice, went to the casino and won $10, got a new apartment, worked on two side web app projects, went to bed at 2am Sunday night to get back up at 5:30am for work. FUN!!!! FUN!!!
by Fred Mastro
16. June 2009 14:20
I have some pages I work on for a project where if the user does not have access to modify the page, we disable all the controls. A ReadOnly version.
Well the current system had a method that went control by control, disabling or setting to readonly for textboxes and setting buttons to visible false.
So anyway, I was working on a new page and thought, hell no I’m going to manually type in all those controls and disable it all. So I wrote my own function.
So use it if you like. It basically is similar to my cascading findcontrol function. I loop through controls and controls of controls and disable, hide or whatever based on the type of control it is. You may want to add more types of controls.
Here is the function you would fun. Sending it a Server Control and telling it if you want it to cascade or not.
''' <summary>
''' Loops through each control in the sent control and disables each control if it can.
''' </summary>
''' <param name="ControlContainer">Server control to check for child controls</param>
''' <param name="DisableControlsOnEachChildControl">If true, then recursively disable all the controls for each child control</param>
''' <remarks></remarks>
Protected Sub DisableAllControlsInGivenControl(ByVal ControlContainer As Control, Optional ByVal DisableControlsOnEachChildControl As Boolean = False)
For Each Control In ControlContainer.Controls
If DisableControlsOnEachChildControl AndAlso (Control.Controls IsNot Nothing AndAlso Control.Controls.Count > 0) Then
DisableAllControlsInGivenControl(Control, True)
End If
DisableControl(Control)
Next
End Sub
Once you have that on your page, you’ll notice it has a supporting method. DisableControl. This is the meat and potatoes of it. Here is where you would want to make your changes if you wanted to add more controls.
''' <summary>
''' This will take any control type that inherited from ui.webcontrol and attempt to disable it.
''' Good if you want to loop through all controls in a container and disable everything.
''' </summary>
''' <param name="ControlToDisable">The Control You wish to Disable (if it inherits from the WebControl Class)</param>
''' <remarks></remarks>
Private Sub DisableControl(ByVal ControlToDisable As Control)
DisableControl(ControlToDisable, GetType(Object))
End Sub
''' <summary>
''' Will take a control and then check to see if it's of a certain system.type and then will try to disable it.
''' Good if you want to loop through a container and disable all X type controls.
''' </summary>
''' <param name="ControlToDisable">The Control You wish to Disable (if it inherits from the WebControl Class)</param>
''' <param name="ControlType">The System.Type of Control you wish to verify that the ControlToDisable is of said Type</param>
''' <remarks>Ex. Disable Control if it's a RadioButton: DisableControl(SomeControl, GetType(RadioButton))</remarks>
Private Sub DisableControl(ByVal ControlToDisable As Control, ByVal ControlType As Type)
If (ControlToDisable.GetType() Is ControlType) OrElse (ControlType Is GetType(Object)) Then
Dim control As Object = CType(ControlToDisable, Object)
Try
If (ControlToDisable.GetType() Is GetType(ImageButton)) Then 'If ImageButton, then visible false
CType(control, ImageButton).Visible = False
ElseIf (ControlToDisable.GetType() Is GetType(Button)) Then 'If Button then visible false
CType(control, Button).Visible = False
ElseIf (ControlToDisable.GetType() Is GetType(TextBox)) Then 'If textbox then set ReadOnly
CType(control, TextBox).ReadOnly = True
Else
control.Enabled = False 'Else just disable
End If
Catch ex As Exception
End Try
End If
End Sub
Enjoy. Got a better way? Let me know.
Update: For those of you who asked for it. Here’s an EnableAllControlsInGiven…. I didn’t need this as controls are only disabled is you don’t have access.
''' <summary>
''' Loops through each control in the sent control and Enables each control if it can.
''' </summary>
''' <param name="ControlContainer">Server control to check for child controls</param>
''' <param name="EnableControlsOnEachChildControl">If true, then recursivly Enable all the controls for each child control</param>
''' <remarks></remarks>
Protected Sub EnableAllControlsInGivenControl(ByVal ControlContainer As Control, Optional ByVal EnableControlsOnEachChildControl As Boolean = False)
For Each Control In ControlContainer.Controls
If EnableControlsOnEachChildControl AndAlso (Control.Controls IsNot Nothing AndAlso Control.Controls.Count > 0) Then
EnableAllControlsInGivenControl(Control, True)
End If
EnableControl(Control)
Next
End Sub
by Fred Mastro
16. June 2009 12:48
In asp.net instead of
Response.End
Use
HttpContext.Current.ApplicationInstance.CompleteRequest()
by Fred Mastro
15. June 2009 11:02
Sometimes your asp.net pages work better when you don't accidently delete the whole Page_Load method.
a48a9215-92d2-4cce-a42b-3b5a034dd50d|2|3.5
Tags: tips
Tips
by Fred Mastro
5. June 2009 15:50
There are many examples out there of cascading methods to find a control nested down in another control which is in some other control. So here’s my version. Maybe there’s better ways, but this one works for me. Returns Nothing if it’s not found. So capture for that.
''' <summary>
''' Does a FindControl on the given Control and if not found,
''' cascades down into the controls of the control until
''' found or no more controls and returns nothing
''' </summary>
''' <param name="StartingControl">Control to start with</param>
''' <param name="FindThisID">ID of control you are looking for</param>
''' <returns></returns>
''' <remarks></remarks>
Private Function FindControlCascading(ByVal StartingControl As Control, _
ByVal FindThisID As String) As Control
Try
If StartingControl.FindControl(FindThisID) Is Nothing Then
If StartingControl.Controls.Count > 0 Then
For Each objControl In StartingControl.Controls
FindControlCascading(objControl, FindThisID)
Next
End If
Else
Return StartingControl.FindControl(FindThisID)
End If
Return Nothing
Catch ex As Exception
Throw ex
End Try
End Function
Got a better way? I’d love to hear it.
by Fred Mastro
3. June 2009 11:59
I had desire to populate a DropDownList with values from a resource file. Using the resource name as the ddl value and the resource value as ddl text. There’s other reasons I need to do this but I’m simplifying it here.
The resource file name is: “BatchFields_ADMRACClaim”
The DropDownList is called “ddlFeilds”
So first I get the resource file object and then I do a GetType() to initialize it. If anyone has any other suggestions for this, please let me know. Without the GetType() line or pulling out a value first, the GetResourceSet later is Nothing.
Dim BatchFileResource As ResourceManager
BatchFileResource = Resources.BatchFields_ADMRACClaim.ResourceManager()
Dim Value As String = BatchFileResource.GetType.Assembly.FullName
Then once I have the resource file in the “BatchFileResource” object, I can then loop through the items and dump them into a DropDownList.
Dim BatchResourceSet As ResourceSet = BatchFileResource.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, True, False)
Dim BatchEnumerator As IDictionaryEnumerator = BatchResourceSet.GetEnumerator()
ddlFields.Items.Clear()
While BatchEnumerator.MoveNext
Dim ddlItem As New ListItem(BatchEnumerator.Value, BatchEnumerator.Key)
ddlFields.Items.Add(ddlItem)
End While
And that’s it. I’ve found other samples on the web but I’ve combined the best of both worlds here.
by Fred Mastro
21. May 2009 10:13
Site’s been down for two days, I apologize to my few fans :p haha. Hosting server had issues.
4ff2afd8-302b-4dbc-82fa-f0873885c427|0|.0
Tags:
My Website
by Fred Mastro
18. May 2009 22:12
Well for MSDN Subscribers, Visual Studio 2010 Beta is live and available.
Jason Short posted a blog over here about it.
Some of the new features he stated:
- .Net 4.0
- Cloud Development - Windows Azure which is basically utility computing where you pay by the hour for your usage.
- Parallel Development - Not sure if this only means PLINQ for .Net developers or if they are only talking about C++ devs.
- TDD Improvements - Editor changes for building test cases first, and then writing the code.
- ASP.NET - CSS grids and design surfaces changes, more Javascript debugging improvements.
- C++ Compiler - Lots of changes for C++ development, including MFC.
- Sharepoint templates
- New Project Templates
- Windows 7 - MFC updates for Ribbon UI, and WPF changes.
- Application Model Changes - Reverse existing codebases, and lots of changes in Architect Edition to support more model types (including Class Libraries finally).
- Test Runner - This is potentially huge as it allows testers to submit bug reports complete with callstacks and traces that you can actually use to resume an app at the error point to avoid the dreaded No Repro on bug reports.
Here the offical site, and the VS 2010 MSDN Channel with videos.
The last feature, Test Runner, sounds awesome!
by Fred Mastro
15. May 2009 07:46
Well I've sent away for my U.S. Passport so that I can take a trip somewhere. Not sure where I'd go yet, but open to suggestions! Wold rather go where there is someone I somewhat know or met online, rather then go somewhere where I know no one.
