Page_Load vs Button_Click

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi;

The examples I have seen all use "if (IsPostBack) { ... }" to handle an
action when the user presses the submit button.

However, it seems to me a more object oriented approach is to use a callback
event attached to a button.

Is there a good reason to use one over the other?
 
Actually, most of the time you'll see both. The IsPostBack is usually
checked for in the Page_Load event to avoid unnecessary calls to the db, for
example to populate a datalist when you don't need it populated because of
the actions your about to do with the button. You are correct, it's best to
handle as much as you can with a button handler because it provides the
greatest flexibility. Checking for a button firing in the Page_Load works
nicely, but only if you're going to have one button, adding a second will
just mess things up unless your approach is taken and good object-oriented
design is used.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
David said:
Hi;

The examples I have seen all use "if (IsPostBack) { ... }" to handle an
action when the user presses the submit button.

However, it seems to me a more object oriented approach is to use a callback
event attached to a button.

Is there a good reason to use one over the other?

When there are too many events, your Page_Load (or OnLoad) becomes very
big. It's better to use ASP.NET's event handling mechanism and
Page_Load should be used for page-related initialization only.
 
Back
Top