LoadControl on Page_PreRender

O

Ohad Ezer

Hi

I'm wondering if someone can help me with this.

I have code that dynamcally loads a control (via LoadControl). When
the code is run on the Page_Load event it loads the control and
everything is fine. When I try to run the same code on the Page's
Pre_Render, the control loads up, but the buttons inside it don't do
anything on the first click. After the first click, the page posts
back and then the loaded control works fine.

It seems like loading a control on Pre Render misses something (maybe
to do with ViewState? )

1. ANy ideas how to solve this (I need to load the control on Pre
Render) ?
2. Does anyone know what the loaded control misses between Page Load
and Pre Render?

Please see code example below.

Thank you

Ohad Ezer

This code DOESN'T WORK

private void MyFrom_PreRender(object sender, System.EventArgs e)
{
MyPlaceHolder.Controls.Add(LoadControl("MyControl.ascx"))
}


----------------------------------------->

This code WORKS
private void Page_Load(object sender, System.EventArgs e)
{
MyPlaceHolder.Controls.Add(LoadControl("MyControl.ascx"))
}
 
T

Teemu Keiski

Hi,

on postback the control would need to be loaded at Page_Load at the latest
for postback events to be raised. The page lifecycle will explain this you:

1. Instantiate
2. Initialize
3. TrackViewState
4. LoadViewState (postback)
5. Load postback data (postback, IPostBackDatahandler.LoadPostdata)
6. Load
7. Load postback data for dynamical controls added on Page_Load
8. Raise Changed Events (postback,
IPostBackDatahandler.RaisePostDataChanged)
9. Raise postback event (postback, IPostBackEventHandler.RaisePostBackEvent)
10.PreRender
11. SaveViewState
12. Render
13. Unload
14. Dispose

As you can see. postback events are raised just after Page_load, so controls
need to exist at that time ( at Load, because actual postback data loading
happens in phase 7)

If you can't change where the control is added, you need to manually inspect
from Form post (Request.Form) collection, which button was clicked and raise
the event.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist

Hi

I'm wondering if someone can help me with this.

I have code that dynamcally loads a control (via LoadControl). When
the code is run on the Page_Load event it loads the control and
everything is fine. When I try to run the same code on the Page's
Pre_Render, the control loads up, but the buttons inside it don't do
anything on the first click. After the first click, the page posts
back and then the loaded control works fine.

It seems like loading a control on Pre Render misses something (maybe
to do with ViewState? )

1. ANy ideas how to solve this (I need to load the control on Pre
Render) ?
2. Does anyone know what the loaded control misses between Page Load
and Pre Render?

Please see code example below.

Thank you

Ohad Ezer

This code DOESN'T WORK

private void MyFrom_PreRender(object sender, System.EventArgs e)
{
MyPlaceHolder.Controls.Add(LoadControl("MyControl.ascx"))
}


----------------------------------------->

This code WORKS
private void Page_Load(object sender, System.EventArgs e)
{
MyPlaceHolder.Controls.Add(LoadControl("MyControl.ascx"))
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top