Page_Load vs Button_Click

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?
 
M

Mark Fitzpatrick

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
 
A

Aquila Deus

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top