2

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.

CptSupermrkt
  • 6,844
  • 12
  • 56
  • 87

4 Answers4

1

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.

Andrej K
  • 1,794
  • 1
  • 17
  • 37
0

If it is not a Postback, it means that the user just requested a page (HTTP Get request) with typing the URL in the browser (or clicking a link, or...).

If the user clicked on a button, then the browser makes a HTTP Post request, which on the server side, ASP.NET marks the page that is requested with setting the property IsPostBack to true of the page object.

See the answer on this question.

Community
  • 1
  • 1
Gorgi Rankovski
  • 2,303
  • 1
  • 23
  • 32
0

The problem is, the event handler can only trigger when it IS a postback. The wiring of event handlers affects this instance of the class only. When a postback occurs, a new instance of your page is created and the event handler is no longer wired. You need to wire it in order for it to get triggered.

The best thing to do is always wire the event handlers. There's no reason not to.

Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
0

I guess what you intend to do is disable the button on postback. You can do that by setting the enabled property to false.

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        myButton.Enabled = false;
    }
}
Ozgur Ozturk
  • 1,265
  • 11
  • 9