Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7801563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:59:18+00:00 2026-06-02T00:59:18+00:00

Let’s say we have a pretty standard form with a textbox and a button

  • 0

Let’s say we have a pretty standard form with a textbox and a button (for simplicity). You want to handle a Click event and do some stuff based on user’s input.

I was wondering, does it matter, when exactly you wire up an event handler for the Click event in a code-behind? If it does, where is the best place to put it? Page load? Page init? I’ve tried both places, but didn’t notice any difference. Or it’s just a personal preference of the programmer? I’ve already searched the internet couple of times, but haven’t found any satisfactory answer.

I know when the actual method execute, just not sure about the wiring-up part.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:59:20+00:00Added an answer on June 2, 2026 at 12:59 am

    As you know, there are several Page_xxx event handlers, like Init, Load, Prerender… This events exist in Controls, and Pages as well as User controls (in fact they’re derived form Control, which holds all these events).

    This events are related to the ASP.NET Page Life Cycle

    If you read the page pointed to by this link carefully you will understand when the events are triggered. So, if you bind your event handler in any page lifecycle event that happens before the events are triggered, it’s guaranteed that your event handlers will be bound in time to be triggered.

    These are the main lifecycle steps:

    PreInit -> Init -> InitComplete -> PreLoad -> Load -> [Control events] ->
    LoadComplete -> PreRender -> SaveStateComplete -> Render -> Unload
    

    Not all of them have associated events, but, if it’s necessary you can override the corresponding OnXxx() function, like OnPreInit(). (This is usually only done on custom server controls).

    You can bind events in Page_Init or Page_Load, because the control events are triggerd after the loading of all the controls has finished. The Load step happens in top-bottom way, first in the Page, and then recursively in all the children controls.

    After Load finishes, the first events which are triggered are the Change Events, like TextChanged or SelectionChanged. Then are triggered all the other events, like Click.

    If you bound the events in PreRender or Unload, they wouldn’t be triggered. If you did in Init or Load, they would.

    So it could look like it’s safe to bind in Init or Load, but that’s not true:

    It could look like there’s no special reason to bind them on Init or Load, because they’ll be triggered later in the page life cycle. But, as the binding defined in the .aspx happens during Init, a programmer will expect that all events are already bound in the Load event. What would happen if this programmer raised an event of a child control in code behind? The Load event happens first in the root of the control tree, and them on all of the children, recursively. So, by the time the programmer is trying to raise the event of the child control, it won’t be already bound. So this won’t work as expected. This is more than enough to consider unsafe to bind events in Load event. That’s why you should always bind events in Init.

    Look at this diagram to see the order of execution of Page & children events:
    ASP.NET Page Life Cycle Diagram

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I want to have some kind of a cache that did something
Let's say you create a wizard in an HTML form. One button goes back,
Let's say I have a drive such as C:\ , and I want to
Let's say I have the string: hello world; some random text; foo; How could
Let say I have some code HTML code: <ul> <li> <h1>Title 1</h1> <p>Text 1</p>
Let's say I have some json like this in mongo: {n:5} and a java
Let's say we have this code: <form action='' method='POST' enctype='multipart/form-data'> <input type='file' name='userFile'><br> <input
let's say I have a class that does some calculations. This set of calculations
Let's say i have two tables in db: Car and Part. Car owns arbitrialy
Let’s say I have a number like 0x448 . In binary this is 0100

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.