The best explanation I’ve found for a postBack is from Wiki.
a postback is an HTTP POST to the same page that the form is on.
While the article does explain how a second page was needed in ASP, but no longer needed in ASP.NET, it doesn’t give much detail or background. I am looking for a freakin’ tome of information on PostBacks. Much like the simple question of “how can I clean a house” can be addressed by this 900 page book. I don’t need 900 pages worth, but details please. I found a nice little tutorial for ASP.NET life cycle, but it seriously glosses over postbacks (amongst other things).
I am looking to the developers who have been around before .NET and really don’t take these kinds of things for granted. Books and hyperlinks are reasonable answers or additions to your answer.
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
So far I’ve seen the right answer alluded to repeatedly, and almost everyone has come shy of what I consider subjectively to be the mark.
Let’s start with the basics:
An HTTP request can be any of the HTTP verbs, but the two that people use most are GET and POST. Well, those are the two a programmer uses most frequently. The others all have some purpose, if they’re implemented on the server. When you send information to the server, you can do so either through the use of the URL (to request a page) or within the body of the request (POST, PUT, DELETE, for instance).
Now you’ll remark (I’m sure) that the URL in a GET request often contains data, and this is true, but according to W3C, you should not use GET to alter state, and yet we do often. It’s sort of a hack that we all agree is an actual use, and not a hack. Whether that makes it a hack or an actual implementation detail I leave up to you.
So when you send the body of the POST (skipping the others for now, you can figure it out from here) with the form elements, you’re sending back certain elements. How those elements are defined is up to you and to the environment you’re working in. You could post to a server with a JSON element in the body, or with XML, or with form fields. Generally we do posts from a FORM element in the body of the HTML.
Now everyone says, “oh, a postback is a subsequent request to a page.” But, that’s not true. A postback is when you send data via POST -> back to the server. I say this because the difference between a GET request and a POST request is if data is included in the body (and the verb used, but the client usually knows how to deal with that). You could postback to the page on the first time the page is visited, and in fact ASP.NET has tools for doing that in the library. You could certainly have a desktop client POST data to a server (think Twitter) without showing any webpage at all from the server (ok, so twitter is probably not the best concept to use for an example here, but I want to illustrate that you can use a client that doesn’t show the webpage, so no request is necessary).
So really what you should read there in “postback” is “I’m POSTing data BACK to the server for processing”. It’s presumed that you retrieved the page initially with a GET to show the user the
<form> element that has
<input> fields for them to interact with, and that at the end you’re sending data back. But I hope you can see that it doesn’t have to be in that order.
So here’s something else to consider:
What if you gave the user a page with a bunch of
<input>s and no
&value-n= and send them as a GET? Does the same thing, but violates that concept of only using GET for requests.
(possibly) ensuing discussion encourages me to reinforce that GET should have no side effects (no updating values)
It’s how come you can send someone a link to a google search, for instance. So we don’t ALWAYS have to POST BACK to the server to get data.
Hope this helps.
See ASP.NET Page Life Cycle Overview on MSDN for a good general introduction about what happens when a requests hits the server.
A PostBack is any request for a page that is not the first request. A PostBack will always be in response to a user action (triggered most commonly by a Button, AutoPostBack control or Ajax).
POSTBACK: Part of ASP.NET’s contrived technique for hiding the true stateless nature of the web/HTTP behind a stateful facade. This results in complex code (IsPostback, …), a hard to understand page lifecycle, many different events, … and numerous problems (ViewState size, web-farm stickyness, state servers, browser warnings (not using PRG pattern), …)
See ASP.NET MVC instead.
A post back is round trip from the client (Browser) to the server and then back to the client.
This enables you page to go through the asp engine on the server and any dynamic content to be updated.
ASP.Net uses a new concept (well, new compared to asp… it’s antiquated now) of ViewState to maintain the state of your asp.net controls. What does this mean? In a nutshell, if you type something into a textbox or select a dropdown from a dropdownlist, it will remember the values when you click on a button. Old asp would force you to write code to remember these values.
This is useful when if a user encounters an error. Instead of the programmer having to deal with remembering to re-populate each web control, the asp.net viewstate does this for you automatically. It’s also useful because now the code behind can access the values of these controls on your asp.net web form with intellisense.
As for posting to the same page, yes, a “submit” button will post to an event handler on the code behind of the page. It’s up to the event handler in the code behind to redirect to a different page if needs be (or serve up an error message to your page or whatever else you might need to do).
The Wikipedia definition of postback is pretty good, but I’d add the following:
A postback is a subsequent HTTP POST to the same page that the form is on.
If I have a page with a form on it and, rather than having my Submit button redirect the browser to another page that will process the form, instead have the Submit button refresh the current page (and perform some specific steps to validate/save the page, presumably), then that Submit button is said to have posted back to the current page.
Postbacks can be either full (refresh the entire page) or partial (in a case where AJAX is employed). A partial page postback will re-render only a part of the page (like a single drop-down list, a table, etc.).
In the old HTML, the only way to make something updated on the webpage is to resend a new webpage to the client browser. That’s what ASP used to do, you have to do this thing call a “PostBack” to send an updated page to the client.
In ASP .NET, you don’t have to resend the entire webpage. You can now use AJAX, or other ASP.NET controls such that you don’t have to resend the entire webpage.
If you visit some old website, you would notice that once you click something, the entire page has to be refresh, this is the old ASP. In most of the modern website, you will notice your browser doesn’t have to refresh the entire page, it only updates the part of the content that needs to be updated. For example, in Stackoverflow, you see the page update only the content, not the entire webpage.
Simply put this by a little code. Hope it is helpful to you.
When you firstly request the page url. you can view the source code of it in most browser. Below is a sample of it .
The essential of Post Back is actually call the
__doPostBack which submit all the form data got from your firstly requested back to the server. (
__EVENTTARGET contains the id of the control.)
Postback is a request during which ASP restores values of controls’ properties from view state.