85

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.

dariom
  • 4,413
  • 28
  • 42
P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348

9 Answers9

62

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 <form> but instead, had a button wired up in javascript to concat all those <input>s with &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. Cheers

Mycah
  • 4,602
  • 5
  • 24
  • 32
jcolebrand
  • 15,889
  • 12
  • 75
  • 121
  • 1
    `Does the same thing, but violates that concept of only using GET for requests. (possibly)` No possibly about it. GET requests should not have side effects. They should merely request data (hence the name **GET**). – George Marian Nov 22 '10 at 23:46
  • 34
    -1: The question is tagged ASP.NET. In ASP.NET the term "Postback" has a specific meaning involving page life cycle events, Viewstate, testing IsPostBack, ... – Ian Mercer Nov 23 '10 at 00:56
  • @high M$ may try to redefine w/e they want. However, that doesn't mean we have to accept it. – George Marian Nov 23 '10 at 01:38
  • @hightechrider can you explain the parts of that cycle without that explanation I give? – jcolebrand Nov 23 '10 at 01:39
  • 2
    So basically, a postback is just a HTTP POST to any page? – Koray Tugay May 18 '16 at 06:28
  • @KorayTugay more or less, there are a few nuances, but yes. – jcolebrand May 23 '16 at 20:20
  • While this is all true, I think i have to agree this answer could do with some cognisance/acknowledgement of the nuance surrounding how ASP.Net treats POST Backs and their special meaning within that platform. Regardless of whether or not we like or agree with the standards M$ likes to play fast and loose with, the reality is they have, they do, and it's important to understand how a specific platform works if you are working in it. Hopefully future readers look down further for the missing bits and morsels. – DavidScherer Feb 15 '19 at 14:42
  • Hi @DavidScherer, this is a wiki site. Feel free to add clarification and improve life for everyone. – jcolebrand Feb 15 '19 at 21:48
28

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).

Community
  • 1
  • 1
Yaakov Ellis
  • 40,752
  • 27
  • 129
  • 174
  • 1
    Even though this is an old topic, I wanted to point out that your answer is partially incorrect (so others are not mislead). You can point the form action to any URL you wish, in that it could be the first request. What is important is that it is a POST. – Michael Goss Oct 30 '16 at 01:41
27

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.

Ian Mercer
  • 38,490
  • 8
  • 97
  • 133
  • 3
    Wow, really? I'm pretty sure I've heard postback used in references to other frameworks besides asp.net .... Let's not confuse the poor lad. – jcolebrand Nov 22 '10 at 23:23
  • 3
    The question is tagged ASP.NET. In the context of ASP.NET, "Postback" has a specific meaning. See for example: http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx – Ian Mercer Nov 23 '10 at 00:52
  • And outside the context of ASP.NET postback is still a valid action. – jcolebrand Nov 23 '10 at 01:42
  • You should make it clear that there are other working definitions of the term, if you really want to avoid confusing the OP. – George Marian Nov 23 '10 at 01:42
  • 4
    As a long time non-microsoft web developer, I've never heard the term 'PostBack' outside of .net context. Posting data and the term PostBack are very different things. This is a perfect answer. – tommybananas May 18 '16 at 21:46
  • @jcolebrand true, but this is an ASP.Net question. What would be confusing is explaining a postback without at least mentioning the special nature of them in ASP.net. – DavidScherer Feb 15 '19 at 14:46
13

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.

here is a nice explanation

David Kethel
  • 2,440
  • 9
  • 29
  • 49
10

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).

Scottie
  • 11,050
  • 19
  • 68
  • 109
8

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.).

jwheron
  • 2,553
  • 2
  • 30
  • 40
4

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.

dsum
  • 1,433
  • 1
  • 14
  • 29
  • That's somewhat true, but perhaps a bit misleading. Standard asp.net Postbacks DO send the entire page, plus the viewstate, back to the server and refresh the entire page. AJAX did make this a bit nicer with the ability to only post back portions of the page. – Scottie Nov 22 '10 at 23:20
3

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.)

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
    NHibernate Demo
</title>
    <script language="javascript" type="text/javascript">
        function dopost() {
                __doPostBack('LinkButton1', '');    
        }
    </script>
</head>
<body>
    <h1>NHibernate Demo</h1>    
    <form name="ctl01" method="post" action="Default.aspx" id="ctl01">
<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTMxNzcwNTYyMWRkKHoXAC3dty39nROvcj1ZHqZ5FYY=" />
</div>

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['ctl01'];
if (!theForm) {
    theForm = document.ctl01;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>   
<div>
    <input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="B2D7F301" />
    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwKZx5vTCgKM54rGBgLM9PumD20dn9KQguomfpAOdTG0r9Psa7al" />
</div>
        <a id="LinkButton1" href="javascript:__doPostBack('LinkButton1','')">LinkButton</a>
        <input type="button" value="testPostBack" id="testpostback" onclick="dopost();" />
    </form>
</body>
</html>
Joe.wang
  • 11,537
  • 25
  • 103
  • 180
2

Postback is a request during which ASP restores values of controls' properties from view state.

Waldemar Gałęzinowski
  • 1,125
  • 1
  • 10
  • 18