I'm making my way into web development and have seen the word postback thrown around. Coming from a non-web based background, what does a new web developer have to know about postbacks? (i.e. what are they and when do they arise?)

Any more information you'd like to share to help a newbie in the web world be aware of postbacks would be most greatly appreciated.

11 Answers
11

When does it happen?

A postback originates from the client browser. Usually one of the controls on the page will be manipulated by the user (a button clicked or dropdown changed, etc), and this control will initiate a postback. The state of this control, plus all other controls on the page,(known as the View State) is Posted Back to the web server.

What happens?

Most commonly the postback causes the web server to create an instance of the code behind class of the page that initiated the postback. This page object is then executed within the normal page lifecycle with a slight difference (see below). If you do not redirect the user specifically to another page somewhere during the page lifecycle, the final result of the postback will be the same page displayed to the user again, and then another postback could happen, and so on.

Why does it happen?

The web application is running on the web server. In order to process the user’s response, cause the application state to change, or move to a different page, you need to get some code to execute on the web server. The only way to achieve this is to collect up all the information that the user is currently working on and send it all back to the server.

Some things for a beginner to note are...

The state of the controls on the posting back page are available within the context. This will allow you to manipulate the page controls or redirect to another page based on the information there.

Controls on a web form have events, and therefore event handlers, just like any other controls. The initialisation part of the page lifecycle will execute before the event handler of the control that caused the post back. Therefore the code in the page’s Init and Load event handler will execute before the code in the event handler for the button that the user clicked.

The value of the “Page.IsPostBack” property will be set to “true” when the page is executing after a postback, and “false” otherwise.

Wow! Great description! Thanks so much for the detail. This really helps.
–
Scott SaadOct 8 '08 at 16:03

2

Small correction - "The state of this control, plus all other controls on the page,(known as the View State) is Posted Back." This data is actually the Postback data. Viewstate data is also posted back but that is the state of the controls the last time they were on the server.
–
Simon KeepOct 20 '08 at 16:15

Note postback is used on all .NET platforms, not just ASP.NET
–
JNFJul 20 '12 at 7:59

1

"In order to process the user’s response, cause the application state to change, or move to a different page, you need to get some code to execute on the web server." This is a little misleading. You don't always need 'code to execute on the web server' to do these things. When possible, it can be better to process user input on the client. One of the most important things to know about post-backs is how/when not to do them; i.e., when to take care of things client-side, or via Ajax, etc.
–
Hawkeye ParkerNov 7 '13 at 18:37

1

Agree @HawkeyeParker. I wrote this back when all I knew was Web Forms architecture. Things have certainly come a long way since then! All of my explanation definitely doesn't take Ajax and into account at all.
–
Andy McCluggageNov 13 '13 at 16:08

A Postback is an action taken by an
interactive webpage, when the entire
page and its contents are sent to the
server for processing some information
and then, the server posts the same
page back to the browser.

Expanding on the definitions given, the most important thing you need to know as a web-developer is that NO STATE IS SAVED between postbacks. There are ways to retain state, such as the Session or Viewstate collections in ASP.NET, but as a rule of thumb write your programs where you can recreate your state on every postback.

This is probably the biggest difference between desktop and web-based application programming, and took me months to learn to the point where I was instinctively writing this way.

The term is also used in web application development when interacting with 3rd party web-service APIs

Many APIs require both an interactive and non-interactive integration. Typically the interactive part is done using redirects (site 1 redirects a user to site 2, where they sign in, and are redirected back). The non-interactive part is done using a 'postback', or an HTTP POST from site 2's servers to site 1's servers.

Web developement generally involves html pages that hold forms (<form> tags). Forms post to URLs. You can set a given form to post to any url you want to. A postback is when a form posts back to it's own page/url.

The term has special significance for ASP.Net developers, because it is the primary mechanism that drives a lot of the behavior for a page -- specifically 'event handling'. ASP.Net pages have exactly one server form that nearly always posts back to itself, and these post backs trigger execution on the server of something called the Page Lifecycle.

Postback refers to HTML forms. An HTML form has 2 methods: GET and POST. These methods determine how data is sent from the client via the form, to the server. A Postback is the action of POSTing back to the submitting page. In essence, it forms a complete circuit from the client, to the server, and back again.

Yet the question is answered accurately above, but just want to share my knowledge .
Postback is basically a property that we can use while doing some tasks that need us to manage the state of the page, that is either we have fired some event for e.g. a button click or if we have refreshed our page.
When our page loads for the very first time , that is if we have refreshed our page, at that time postback-property is false, and after that it becomes true.