UserControl的事件处理程序没有触发

时间:2022-12-29 15:50:33

I dynamically load a UserControl into a View that's in a MultiView control. Although the UserControl adds an event handler, the event never fires.

我动态地将UserControl加载到MultiView控件中的View中。虽然UserControl添加了一个事件处理程序,但事件永远不会触发。

What am I missing here? Thanks!

我在这里想念的是什么?谢谢!

Containing ASPX page:

包含ASPX页面:

protected override void OnPreRender(EventArgs e)
{
    if (MultiView1.ActiveViewIndex == 2) //If the tab is selected, load control
    {
        Control Presenter = LoadControl("Presenter.ascx");
        (MultiView1.ActiveViewIndex.Views[2].Controls.Add(Presenter);
    }
    base.OnPreRender(e);
}

Presenter.ascx.cs

override protected void OnInit(EventArgs e)
{
    Retry.Click += this.Retry_Click; //This is a .Net 2.0 project
    base.OnInit(e);
}


protected void Retry_Click(object sender, EventArgs e)
{
    //This never fires
}

4 个解决方案

#1


I am thinking it is not firing because you are loading the control in your page's prerender event. Upon postback, the control is being lost because there is no view state for it. Therefore there is no control to fire its event. Try to load the control in the page's init event. Let us know what happens!

我认为它没有触发,因为你正在页面的prerender事件中加载控件。在回发时,控件正在丢失,因为它没有视图状态。因此无法控制其事件。尝试在页面的init事件中加载控件。让我们知道会发生什么!

#2


Postback event handling is done before rendering so the control is not present in the page in your case.

在渲染之前完成回发事件处理,因此在您的案例中页面中不存在控件。

The life cycle events are fired in this order (skipped a few):

生命周期事件按此顺序触发(跳过几个):

  1. Init
  2. Load
  3. PreRender
  4. Unload

And event handling is done between Load and PreRender (in case some events change the way the page should be rendered, it makes sense).

事件处理在Load和PreRender之间完成(如果某些事件改变了页面的呈现方式,则有意义)。

So just move your code that loads the Retry control to Load or Init.

因此,只需将加载Retry控件的代码移动到Load或Init即可。

Reference: Asp.Net Page Life Cycle Overview

参考:Asp.Net页面生命周期概述

#3


The control must be visible initially to be able to enter in OnPreRender event. but maybe you want it to be unvisible. the be sure to have EnableViewState = false;

控件必须最初可见才能进入OnPreRender事件。但也许你想让它变得不可见。一定要有EnableViewState = false;

#4


It sounds like the control is not being added after each post back, i would take out the if statement in the containing aspx page to see if that fixes the issue...im assuming Retry is a button?

听起来好像每个帖子后面都没有添加控件,我会在包含aspx页面中取出if语句来查看是否能解决问题...我假设Retry是一个按钮?

#1


I am thinking it is not firing because you are loading the control in your page's prerender event. Upon postback, the control is being lost because there is no view state for it. Therefore there is no control to fire its event. Try to load the control in the page's init event. Let us know what happens!

我认为它没有触发,因为你正在页面的prerender事件中加载控件。在回发时,控件正在丢失,因为它没有视图状态。因此无法控制其事件。尝试在页面的init事件中加载控件。让我们知道会发生什么!

#2


Postback event handling is done before rendering so the control is not present in the page in your case.

在渲染之前完成回发事件处理,因此在您的案例中页面中不存在控件。

The life cycle events are fired in this order (skipped a few):

生命周期事件按此顺序触发(跳过几个):

  1. Init
  2. Load
  3. PreRender
  4. Unload

And event handling is done between Load and PreRender (in case some events change the way the page should be rendered, it makes sense).

事件处理在Load和PreRender之间完成(如果某些事件改变了页面的呈现方式,则有意义)。

So just move your code that loads the Retry control to Load or Init.

因此,只需将加载Retry控件的代码移动到Load或Init即可。

Reference: Asp.Net Page Life Cycle Overview

参考:Asp.Net页面生命周期概述

#3


The control must be visible initially to be able to enter in OnPreRender event. but maybe you want it to be unvisible. the be sure to have EnableViewState = false;

控件必须最初可见才能进入OnPreRender事件。但也许你想让它变得不可见。一定要有EnableViewState = false;

#4


It sounds like the control is not being added after each post back, i would take out the if statement in the containing aspx page to see if that fixes the issue...im assuming Retry is a button?

听起来好像每个帖子后面都没有添加控件,我会在包含aspx页面中取出if语句来查看是否能解决问题...我假设Retry是一个按钮?