Init method firing early

G

gozza

Hi,

Looks like I've misunderstood something about controls lifecycles. I have
an OnInit method override in my control:

protected override void OnInit(EventArgs e)
{
base.OnInit (e);
//some stuff
}

The control is then added to an ASPX page and an event handler is assigned
in the Page's OnInit override (as it's usually done by VS.NET, in the
InitilizeComponent method). The problem is that my event handler does not
get to do its work since the control's OnInit method always gets called
before the Page's OnInit method (i.e. the control's Init event gets
triggered before the Page's) and as a result, the event handler assignment
is made too late. Can someone please explain to me what's happening?

Any help will be highly appreciated.

Thanks,
Gozza
 
G

gozza

Ok, again I'm answering questions to myself, although I am not certain I
like the options. The only way I can handle the Init event is to create and
add my control in the code-behind file, without making any references to it
in the ASPX page in design mode. Considering my control's functionality,
this will sort of defeat its purpose.

Now, looks like I will have to find another way to do what I need to: it's
sort of like having a drop-down control with both static and dynamic
elements. Some of them would be defined in the ASPX file (so they can be
easily modified), and some would be highly dependant on data stored in a
database + some business rules. The reason I wanted to handle the Init
event is to be able to create the dynamic elements for the drop-down before
any other processing is done for the drop-down (i.e. the Load Postback Data
handling that happens just AFTER the Init and Load View State).

I guess I'll have to publish my own event that I'll trigger before Load
Postback Data or Load View State or Load.

Btw, I found this earlier post:
---------------------------------------
"The Init event for a control created in Design view is handled in the
OnInit for the page. If you want to explicitly handle the Init event, you
need to explicitly create the control by doing it dynamically.

Jim Cheshire [MSFT]
Developer Support
ASP.NET
(e-mail address removed)

This post is provided as-is with no warranties and confers no rights."
 

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

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top