IsPostBack vs. OnClick

R

Random

Is there considered a preferred method for handling the POST from an ASP.NET
web page? I'm a veteran with classic ASP, so have always used
Request.ServerVariables("REQUEST_METHOD") to check for POST. In choosing
between the new IsPostBack and the server side processing for the button
OnClick event, what criteria would be used to decide which method to use?
Or are they both equal, process-wise?
 
D

Dale

Assuming the control that you're clicking has its AutoPostBack property set
to true, then when you click the control, you'll post back. When you post
back, whether by AutoPostBack, or by clicking some other control that causes
the form to be submitted, the process is the same. Page_Load runs first,
and then the event handlers for any pending events occur.

Theoretically, you could handle the entire postback in your Page_Load rather
than in your OnClick handler, but you should use the OnClick event for
processing code that is dependent on the click.

Dale
 
J

John Saunders

Random said:
Is there considered a preferred method for handling the POST from an ASP.NET
web page?

Generally, one handles PostBack in the Page_Load handler:

private void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// Load your data from the database
DataBind();
}
else
{
// Do anything that still needs to be done on a postback, like
binding any controls with
// ViewState turned off
}
}
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top