是否有一个在ASP.net Page_Load事件后, [英] Is there an after Page_Load event in ASP.net

查看:264
本文介绍了是否有一个在ASP.net Page_Load事件后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有正在的所有的Page_Load 事件已完成?

Is there an event that is triggered after all Page_Load events have completed?

我怎么能有一个以上的的Page_Load ?结果
  当你的用户控件。

How can i have more than one Page_Load?
When you have user controls.

在我的网页可以呈现,我需要我的网页(和所有嵌入式控制)通过完成他们的的Page_Load 事件初始化自己。

Before my page can render, i need my page (and all embedded controls) to have initialized themselves by completing their Page_Load events.

问题当然是,如果我把code在我的页面的的Page_Load 处理程序:

The problem, of course, is that if i put code in my page's Page_Load handler:

MyPage.aspx
   --> Page_Load
          ---> DoSomethingWithUserControl()
UserControl1.ascx
   --> Page_Load
          ---> initialize ourselves now that viewstate has been restored

然后我开始访问我的的UserControl1 控制之前它已准备就绪。

then i begin to access my UserControl1 control before it is ready.

我需要一种方式来运行code的之后的所有的Page_Load 事件解雇,但的的任何回发事件(如点击事件)已经解雇了:

i need a way to run code after all Page_Load events have fired, but before any postback events (e.g. Click events) have fired:

MyPage.aspx
   --> Page_Load
UserControl1.ascx
   --> Page_Load
          ---> initialize ourselves now that viewstate has been restored
MyPage.aspx
   --> Page_AfterLoad
          ---> DoSomethingWithUserControl()

看MSDN中的页面生命周期,它看起来像有没有办法引发事件的 的所有Page_Loads已完成后:

Looking at the page lifecycle in MSDN it looks like there is no way to raise an event after all Page_Loads have been completed:

有没有一种方法,以提高后的 的所有Page_Loads完成后?

Is there a way to raise an after after all Page_Loads have completed?

推荐答案

<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.page.loadcomplete.aspx\"><$c$c>Page_LoadComplete是所有控件已经被加载后所引发的事件。

Page_LoadComplete is the event that is raised after all controls have been loaded

记住初始化事件首先由所有子控件触发,就在所有的控制已被初始化,初始化页的事件引发的。在加载事件工作周围的其他方法,页面首先提出了一个加载事件,然后每个子控件提高自己的加载事件。在结束了 LoadComplete 升高。 请注意,在创建控件时动态他们(黯然)没有严格按照这个办法在设计时,创建控件只有当这是真的。

Remember that the Init event is first triggered by all child controls, and just when all controls have been initialized, the Init event of the page is raised. The Load event works the other way around, the page first raises the Load event and then each child control raises its own Load event. At the end the LoadComplete is raised. Note that this is true only when the controls are created at design time, when the controls are created dynamically they (sadly) do not follow this approach strictly.

从MSDN:

如果在运行时动态创建的控件或声明中的数据绑定控件模板,他们的活动最初不与其他控件的页面上同步。例如,对于在运行时增加了一个控制,init和负载事件可能会在页面生命周期远远晚于控件相同的事件声明创建发生。因此,从它们被实例化时,动态添加管制和模板提高他们的事件一前一后,直到它们已经赶上到在此期间它被添加到控制集合的事件。

If controls are created dynamically at run time or declaratively within templates of data-bound controls, their events are initially not synchronized with those of other controls on the page. For example, for a control that is added at run time, the Init and Load events might occur much later in the page life cycle than the same events for controls created declaratively. Therefore, from the time that they are instantiated, dynamically added controls and controls in templates raise their events one after the other until they have caught up to the event during which it was added to the Controls collection.

请看下图:

(来源:<一个href=\"http://msdn.microsoft.com/en-us/library/ms178472.aspx\">http://msdn.microsoft.com/en-us/library/ms178472.aspx)

为了满足您的所有需求:

In order to fulfill all your requirements:

我需要一种方式来运行code所有的Page_Load事件后发射,但在此之前的任何回发事件(例如点击事件)已经解雇了:

i need a way to run code after all Page_Load events have fired, but before any postback events (e.g. Click events) have fired:

我觉得最简单的方法是在用户控件声明一个自定义事件,并启动它的控制已被加载后,再强制订阅该事件在你的ASPX

I think the easiest way is to declare a custom event in the User Control and fire it after the control has been loaded, then jus subscribe to that event in your ASPX

    public event Action LoadCompleted = delegate { };

    protected void Page_Load(object sender, EventArgs e)
    {
        this.LoadCompleted();
    }

ASPX页面

    protected void Page_Load(object sender, EventArgs e)
    {
        this.myUserControl.LoadCompleted += () => 
        {
            // do somethign interesting
            this.lblMessage.Text = DateTime.Now.ToString();
        };
    }

这篇关于是否有一个在ASP.net Page_Load事件后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆