This tutorial will explain what Page.IsPostBack actually determines and demonstrate how to use it with ASP.NET 4.0 and C#.

What is a PostBack?

In ASP.NET, when a page is being loaded it determines whether or not it is a post back. An example of a post back would be something like someone clicking a button on the page which causes the page to reload, while a page that is being loaded for the first time would not be a post back.

Creating an Example

To demonstrate using the case of a post back to execute different code, we will create a simple web site that will display whether or not a post back is happening. At this point, I have created a new ASP.NET Empty Web Site. To begin:

  1. Right click the project in your solution explorer.
  2. Select add new item…
  3. Select a web form.
  4. Name it ‘Default.aspx’.
  5. Click add.
  6. Open Default.aspx up to design mode.
  7. Drag and drop a label onto the web form.
  8. Add a break line.
  9. Drag and drop a button under the break line.
  10. Add a break line.
  11. Drag and drop a hyperlink under the break line.
  12. Change the NavigateUrl property to ‘~/About.aspx’.
  13. Change the text to ‘About’.
  14. Right click the project in your solution explorer.
  15. Select add new item…
  16. Select a web form.
  17. Name it ‘About.aspx’.
  18. Click add.
  19. Open About.aspx up to design mode.
  20. Drag and drop a hyperlink onto the web form.
  21. Change the NavigateUrl property to ‘~/Default.aspx’.
  22. Change the text to ‘Home’.

Displaying PostBack Data

Next, we need to add some simple C# code that will allow us to see if the page is a post back or not. To do this, open up Default.aspx.cs and add the following code to the Page_Load event method:

PostBack Examples

Now that we have our simple web site setup, we need to do some testing here that will demonstrate what causes a post back and what does not. To do this:

  1. Load up the web site to the Default.aspx page.
  2. Notice the label says that this was not a post back. This is because this is the first time we loaded the page.
  3. Click the button.
  4. Notice the label says that this was a post back. This is because the page load event was triggered by the button on the page.
  5. Click the about link.
  6. Click the home link.
  7. Notice the label says that this was not a post back. This is because we were redirected to the page from another page.

Now that you have seen an example of what will and will not cause a post back, you may wonder what they are used for. Let’s take a look at a scenario in which you may need to add some code to populate some data onto a web page from a database. In this case, you would want to add the code to the Page_Load event method. However, you would not necessarily need to repopulate the page from the database everytime the page is loaded. In this case, you would really only need to load up this data if the page was not a post back. This is because a post back comes from the page you are already on, meaning the data would already be loaded. However, in the case that you were linked to the page with the data on it from an outside source, you would actually need to get the data from the database. In this scenario, you can make your web site more efficient by placing certain code into an if statement based on the condition of the post back status.
Download Source Files