开发者

Raising the Load event within a dynamic loaded web usercontrol

I need to load a web user control dynamically.

Looking at http://weblogs.asp.net/srkirkland/archive/2007/11/05/dynamically-render-a-web-user-control.aspx, it states that the page lifecycle events are not fired. I thought I might be able to raise the events through reflection. I cannot figure how to fire th开发者_运维问答e events, am I missing something?

Thanks

Podge


You can do something like this before calling RenderControl:

Page page = new Page();

page.Controls.Add(report);

In this case Init method will be called.

an answer given on that link of yours


The standard Load event should fire just fine. The standard ASP.Net control events are raised for usercontrols. If you are wanting to fire events inside your usercontrol from the parent page then you'll want to do something like this:

Inside your usercontrol create an event and wire it up. In this example I'll call it from Page_Load:

 public event EventHandler TestEvent;

protected void Page_Load(object sender, EventArgs e)
{
   if (this.TestEvent != null)
       {
           this.TestEvent(this, e);
       }
}

Inside your parent page wire up the user controls TestEvent:

protected override void OnInit(EventArgs e)
{
    MyUserControl uc = LoadControl("~/PathToUserControl.ascx");
    uc.TestEvent += new EventHandler(MyUserControl_TestEvent);
}

protected void MyUserControl_TestEvent(object sender, EventArgs e)
{
    //this code will execute when the usercontrol's Page_Load event is fired.
}

Hope that helps!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜