ASP.NET page life cycle explanation

ⅰ亾dé卋堺 提交于 2019-11-26 07:26:56

问题


I was asked to explain the ASP.NET page life cycle in an interview some time back. I did explain it to him, but he was not satisfied with my answer. Could someone explain the life cycle to me please?


回答1:


You can see:

  1. ASP.NET Page Life Cycle Diagram,

  2. The ASP.NET Page Life Cycle (by Solomon Shaffer)

  3. Microsoft Developer Network




回答2:


There 10 events in ASP.NET page life cycle and the sequence is :- Init,Load view state,PostBackdata,Load,Validate,Event,Pre-render,Save view state,Render and Unload.

Below is pictorial view of ASP.NET Page life cycle with what kind of code is expected in that event. I would suggest you to read this article on ASP.NET Page life cycle which explains all the 10 events in details and when to use them.




回答3:


I think Microsoft has done the best job of this. I'd just summarize their summary, pulling out the most significant portions of each event.

ASP.NET Page Life Cycle Overview




回答4:


I recommend this page from MSDN:

ASP.NET Page Life Cycle Overview




回答5:


Answer is already posted by others. just sharing the shortcut code to remember the asp.net life cycle stages which I came across in below post.

R-SIL-VPRU

  1. Request
  2. Start
  3. Initialization
  4. Load
  5. Validation
  6. Post back handling
  7. Rendering
  8. Unload

    How to remember asp.net page life cycle in easy way




回答6:


PreInit:

You can:

Check for the IsPostBack property to determine whether this is the first time the page is being processed. Create or recreate dynamic controls. Set master page dynamically. Set the Theme property dynamically. Read or set profile property values. If Request is postback:

The values of the controls have not yet been restored from view state. If you set control property at this stage, its value might be overwritten in the next event. Init:

In the Init event of the individual controls occurs first, later the Init event of the Page takes place. This event is used to initialize control properties. InitComplete:

Tracking of the ViewState is turned on in this event. Any changes made to the ViewState in this event are persisted even after the next postback. PreLoad:

This event processes the postback data that is included with the request. Load:

In this event the Page object calls the OnLoad method on the Page object itself, later the OnLoad method of the controls is called. Thus Load event of the individual controls occurs after the Load event of the page. ControlEvents:

This event is used to handle specific control events such as a Button control’s Click event or a TextBox control’s TextChanged event. In case of postback:

If the page contains validator controls, the Page.IsValid property and the validation of the controls takes place before the firing of individual control events. LoadComplete:

This event occurs after the event handling stage. This event is used for tasks such as loading all other controls on the page. PreRender:

In this event the PreRender event of the page is called first and later for the child control. Usage:

This method is used to make final changes to the controls on the page like assigning the DataSourceId and calling the DataBind method. PreRenderComplete:

This event is raised after each control's PreRender property is completed. SaveStateComplete:

This is raised after the control state and view state have been saved for the page and for all controls. RenderComplete:

The page object calls this method on each control which is present on the page. This method writes the control’s markup to send it to the browser. Unload:

This event is raised for each control and then for the Page object. Usage:

Use this event in controls for final cleanup work, such as closing open database connections, closing open files, etc.




回答7:


Partial Class _Default
    Inherits System.Web.UI.Page
    Dim str As String

    Protected Sub Page_Disposed(sender As Object, e As System.EventArgs) Handles Me.Disposed

        str += "PAGE DISPOSED" & "<br />"
    End Sub

    Protected Sub Page_Error(sender As Object, e As System.EventArgs) Handles Me.Error
        str += "PAGE ERROR " & "<br />"
    End Sub

    Protected Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
        str += "PAGE INIT " & "<br />"
    End Sub

    Protected Sub Page_InitComplete(sender As Object, e As System.EventArgs) Handles Me.InitComplete
        str += "INIT Complte " & "<br />"
    End Sub

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        str += "PAGE LOAD " & "<br />"

    End Sub

    Protected Sub Page_LoadComplete(sender As Object, e As System.EventArgs) Handles Me.LoadComplete
        str += "PAGE LOAD Complete " & "<br />"
    End Sub

    Protected Sub Page_PreInit(sender As Object, e As System.EventArgs) Handles Me.PreInit
        str = ""
        str += "PAGE PRE INIT" & "<br />"
    End Sub

    Protected Sub Page_PreLoad(sender As Object, e As System.EventArgs) Handles Me.PreLoad
        str += "PAGE PRE LOAD " & "<br />"
    End Sub

    Protected Sub Page_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
        str += "PAGE PRE RENDER " & "<br />"
    End Sub

    Protected Sub Page_PreRenderComplete(sender As Object, e As System.EventArgs) Handles Me.PreRenderComplete
        str += "PAGE PRE RENDER COMPLETE " & "<br />"
    End Sub

    Protected Sub Page_SaveStateComplete(sender As Object, e As System.EventArgs) Handles Me.SaveStateComplete
        str += "PAGE SAVE STATE COMPLTE  " & "<br />"
        lbl.Text = str
    End Sub

    Protected Sub Page_Unload(sender As Object, e As System.EventArgs) Handles Me.Unload
        'Response.Write("PAGE UN LOAD\n")
    End Sub
End Class


来源:https://stackoverflow.com/questions/8457297/asp-net-page-life-cycle-explanation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!