views:

1783

answers:

10

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.

+5  A: 

From wikipedia:

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.

Galwegian
Darn--you beat me to it! :-)
Onorio Catenacci
You snooze you lose ;-)
Galwegian
+2  A: 

A post back is anything that cause the page from the client's web browser to be pushed back to the server.

There's alot of info out there, search google for postbacks.

Most of the time, any ASP control will cause a post back (button/link click) but some don't unless you tell them to (checkbox/combobox)

Miles
+3  A: 

When a script generates an html form and that form's action http POSTs back to the same form.

mspmsp
A: 

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.

steve_c
+5  A: 

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.

RB
well put.nothing is saved except in the session and viewstate
Miles
Great point! Yes, this definitely seems like something that takes a bit to wrap one's brain around, when coming from the client programming domain.
Scott Saad
A: 

Postback is essentially when a form is submitted to the same page or script (.php .asp etc) as you are currently on to proccesses the data rather than sending you to a new page.

An example could be a page on a forum (viewpage.php), where you submit a comment and it is submitted to the same page (viewpage.php) and you would then see it with the new content added.

See: http://en.wikipedia.org/wiki/Postback

Morphio
+1  A: 

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.

Joel Coehoorn
+15  A: 

The following is aimed at beginners to ASP.Net...

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.
  • Technologies like Ajax and MVC have changed the way postbacks work.
Andy McCluggage
Wow! Great description! Thanks so much for the detail. This really helps.
Scott Saad
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.
Si Keep
+1  A: 

Postback happens when a webpage posts its data back to the same script/dll/whatever that generated the page in the first place.

Example in C# (asp.net)

...

if (!IsPostback)
   // generate form
else
   process submitted data;
chris
+1 -- I like people who put code in their posts to demonstrate what's going on, even if it seems trivial to most people. Good post.
Pretzel
+3  A: 

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.

Hemant