• Asp.net2.0页面执行顺序


    Asp.net2.0页面执行顺序
    今天碰到一个问题,在MasterPage的OnLoad中加入一个判断,希望在ContentPage的OnLoad之前执行,结果发现MasterPage的Onload在ContentPage的OnLoad后执行,后来把这个判断移动到MasterPage的OnInit中搞定

    在一个单独的页面中,执行顺序为

    1. PreInit
    2. Init
    3. InitComplete
    4. PreLoad
    5. Load
    6. LoadComplete
    7. PreRender
    8. PreRenderComplete

    在页面有MasterPage的时候

    1. ContentPage.PreInit
    2. Master.Init
    3. ContentPage.Init
    4. ContentPage.InitComplete
    5. ContentPage.PreLoad
    6. ContentPage.Load
    7. Master.Load
    8. ContentPage.LoadComplete
    9. ContentPage.PreRender
    10. Master.PreRender

    注意上面高亮的部分,除此之外其他的地方一般是先MasterPage后ContentPage,这里恰恰相反

    那么控件的加载和页面的几个事件的关系是什么呢?

    一般情况下,页面中的控件回先于OnInit执行,亦即

    1.  Master中控件的Init
    2. ContentPage中控件的Init
    3. Master.Init
    4. Content.Init
    5. Content.Load
    6. Master.Load
    7. Master.中用户控件load
    8. ContentPage页面中的用户控件的 page_load

    可以看到控件的Init在Page的Init前执行,所以在Init中我们就可以放心使用页面中的控件了,但是下面的情况不同

    如果你的页面中使用WebControl,或者从WebControl继承的控件,WebControl需要使用CreateChildControls()来加载子控件,这个函数会在这个控件的Init后被调用,也就是说在一个WebControl的OnInit中,是没有办法直接使用其中的控件的,不过我们也可以自己用FindControl把控件加载进来,WebControl的其他方法的加载顺序见下表

    Event Description
    Init You can use the OnInit method to trap this event and initialize member variables and other values.
    LoadViewState You can trap this event to customize how the control retrieves information from the ASP.NET hidden ViewState field.
    Load This event is raised after you create and initialize the control. This is the best place to make the connection to the database or load document content. You can trap the Load event by adding the OnLoad method to the class.
    PreRender This event is raised before ASP.NET renders the control. Any changes to the control's state are saved into the hidden ViewState field.
    SaveViewState This event is raised before the control state is persisted into the hidden ViewState field. You can trap the event to customize how this information is stored.
    Render You can use the Render method to respond to this event and specify the HTML code that represents the component's content.
    Dispose This event is useful for cleanup operations. It's raised before the control is torn down and is the best place to free the resources created during the load phase.
    Unload This event is raised before the control is torn down. The official documentation says not to use this event to perform cleanup, and to rely on the Dispose event instead.

  • 相关阅读:
    终于干掉了默认的输入法, 关于ctfmon.exe文件
    新工作一周的一些记录
    派送Maxthon 2.0 社区预览版本邀请.
    终于决定要跳槽了.
    当QA推出免责条款, 你会怎么看?
    Post by Word 2007, Test. 用word2007来发表一篇随笔.
    一个好的点菜系统
    再见2006
    昨天的实况以及忽悠姐妹花
    在公司我最近都只喝矿泉水
  • 原文地址:https://www.cnblogs.com/eugenewu0808/p/964002.html
Copyright © 2020-2023  润新知