Home All Groups Group Topic Archive Search About

Correct place to add an event handler in code?

Author
1 Aug 2006 3:22 PM
waldenl
I've got a control on my page (in this case a repeater) that I want to
add an event handler to (in this case I want to handle ItemDataBound).
I know I could add the control declaritively, but to be honest, i'm
lazy and want VS 2005 to create the handler definition for me so I want
to use the += convention so it generates the handler stub. Where's the
"correct" place to put this code? In the Page_Load event?

I know it will probably work in several places, but I'm looking for the
"correct" place to use it.

-Walden

Author
1 Aug 2006 4:05 PM
Alessandro Zifiglio
hi Walden,
protected void Page_Init(object sender, EventArgs e)
    {

    }
For dynamic controls creation, Page_Init is the right place to add controls
to the pages controls collection and also hookup any necessary events, this
way your dynamic control will participate well in the pages control life
cycle for the proper loading of viewstate, postback data and you have what.

Regards,
Alessandro Zifiglio
http://www.AsyncUI.net

<wald***@gmail.com> ha scritto nel messaggio
Show quoteHide quote
news:1154445735.666828.185440@h48g2000cwc.googlegroups.com...
> I've got a control on my page (in this case a repeater) that I want to
> add an event handler to (in this case I want to handle ItemDataBound).
> I know I could add the control declaritively, but to be honest, i'm
> lazy and want VS 2005 to create the handler definition for me so I want
> to use the += convention so it generates the handler stub. Where's the
> "correct" place to put this code? In the Page_Load event?
>
> I know it will probably work in several places, but I'm looking for the
> "correct" place to use it.
>
> -Walden
>
Author
1 Aug 2006 4:09 PM
Teemu Keiski
If you add it declaratively in design view, you can also add the event
handler via property browser (select the lighting symbol when control is
selected) by clicking from the list of available events when same handler is
generated for you. If I'd add event handlers in code, I'd put them in OnInit
(Page_Init).

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke


<wald***@gmail.com> wrote in message
Show quoteHide quote
news:1154445735.666828.185440@h48g2000cwc.googlegroups.com...
> I've got a control on my page (in this case a repeater) that I want to
> add an event handler to (in this case I want to handle ItemDataBound).
> I know I could add the control declaritively, but to be honest, i'm
> lazy and want VS 2005 to create the handler definition for me so I want
> to use the += convention so it generates the handler stub. Where's the
> "correct" place to put this code? In the Page_Load event?
>
> I know it will probably work in several places, but I'm looking for the
> "correct" place to use it.
>
> -Walden
>
Author
1 Aug 2006 4:26 PM
WaldenL
Yes, but that only works from w/in design view, not from w/in code view
of the html, and when dealing w/repeaters it's not always easy/possible
to select the repeater control in the design view. And if you select it
by changing the drop down in the properties view then the event button
(lightning bolt) doesn't show up.

-Walden


Teemu Keiski wrote:
Show quoteHide quote
> If you add it declaratively in design view, you can also add the event
> handler via property browser (select the lighting symbol when control is
> selected) by clicking from the list of available events when same handler is
> generated for you. If I'd add event handlers in code, I'd put them in OnInit
> (Page_Init).