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 8336879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:07:38+00:00 2026-06-09T04:07:38+00:00

Works: protected void Page_Load(object sender, EventArgs e) { myButton.Click += new EventHandler(myButton_Click); } Doesn’t

  • 0

Works:

protected void Page_Load(object sender, EventArgs e)
{
    myButton.Click += new EventHandler(myButton_Click);
}

Doesn’t work:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        myButton.Click += new EventHandler(myButton_Click);
    }
}

Now, what I expected in the second example was for the eventhandler to be wired to the button only when it is not a postback (i.e. the first time the page is loaded), and then on any postback, the button would no longer run the method associated with the event. That doesn’t seem to be the case — from the very first load (not a postback), the button does nothing when clicked.

My suspicion is that this is related to the page life cycle — but I’m not quite sure where this falls into that. If I understand correctly, the method associated with the event gets run after the page has posted back (even if you clicked it on the first time the page loads), but I’m referring to the wiring up of the event to the method with the EventHandler delegate, not the actual running of the associated method.

Note: this is purely an attempt to gain a better understanding of what’s going on behind the scenes, and not an attempt to solve a real world problem.

  • 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-09T04:07:39+00:00Added an answer on June 9, 2026 at 4:07 am

    You need to think about how the code is executed with each page request. What happens on the server is that a class instance is created for each request and if the line of code

    myButton.Click += new EventHandler(myButton_Click);

    is conditional it means the event handler is not wired to the event on postback.

    In other words if you write something like

    <asp:Button ID="myButton" runat="server" OnClick="myButton_Click"  />
    

    This translates to the code you wrote and the event gets wired with each request.

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

Sidebar

Related Questions

I have the following code: protected void Page_Load(object sender, EventArgs e) { var openId
I have made something like the following code: protected void Page_Load(object sender, EventArgs e)
I have the following Page_Load function... protected void Page_Load(object sender, EventArgs e) { XmlDataSource1.Data
I'm having a hard time understanding this. Consider the following example: protected void Page_Load(object
I have onPause in activity and works great. This is code @Override protected void
My group and I have an application that will work with protected content files
This works. $(document).ready(function(){ $(.items article).click(function(){ window.location=$(this).find(a).attr(href); return false; }); }); However , when the
I have a new page with the following: The Response.Redirect works, but I don't
In my Javascript I am manually calling __doPostBack('myUpdatePanelId','mycustomeventarg') It works fine except the EventArgs
I am trying to get my website to work the way Twitter.com works. If

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.