[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

ohad

2/13/2004 4:04:00 PM

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"))
}
1 Answer

Teemu Keiski

2/15/2004 11:29:00 AM

0

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

"Ohad Ezer" <ohad@inclarity.co.uk> wrote in message
news:2cd23375.0402130803.3982a55f@posting.google.com...
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"))
}