Click here to Skip to main content
Licence 
First Posted 24 Jan 2006
Views 62,970
Downloads 326
Bookmarked 25 times

The Intricacies of the IsPostBack If-Block in ASP.NET

By ThirstyMind | 24 Jan 2006
An article explaining IsPostBack checking in ASP.NET.
10 votes, 34.5%
1
8 votes, 27.6%
2
3 votes, 10.3%
3
3 votes, 10.3%
4
5 votes, 17.2%
5
2.56/5 - 29 votes
μ 2.56, σa 2.63 [?]

Introduction

This article explains the correct usage of the IsPostBack checking in Asp.Net.

Using the code

The Presence of an if(!IsPostBack) code block in any Asp.Net C# article or book chapter is as common as a bird on a tree branch. But a proper explanation about its usage is as rare as an unicorn. This short article tries to address this issue.

When an aspx page is displayed for the first time, any code within the Page_Load event, including the if(!IsPostBack) is executed. When the same page is displayed in subsequent times, only the code outside the if(!IsPostBack) is executed. The second scenario happens when there is, say a button_click event, which reads some values either from a server control or a local variable of the page and displays the same page with these values.

The enclosed demo project has a Dropdown List box named lstCity and a local boolean variable named whetherIsGood. The Dropdown list box has three possible cities as items – Houston, Dallas and Austin. The Page_Load event sets the SelectedIndex of the listbox to Dallas (by assigning a value of 1) and the whetherIsGood variable to true. This could be done in three different possible ways as shown below. When the button “Set” is clicked it shows the selected item from the list box and the variable value in two text boxes.

Code1:

private void Page_Load(object sender, System.EventArgs e)
{
    //If you change the displayed city say from Dallas 
    //to Austin on the page and click "Set" button, 
    //it correctly shows Austin in the text box. 
    //But the whetherIsGood variable has a value of false.
    if(!IsPostBack)
    {
        lstCity.SelectedIndex = 1;
        whetherIsGood = true;
    }
}

Code 2:

private void Page_Load(object sender, System.EventArgs e)
{
    //If you change the displayed city say from Dallas 
    //to Austin on the page and click "Set" button, 
    //it correctly shows Austin in the text box. 
    //And the variable also correctly has a value of true.
    if(!IsPostBack)
    {
        lstCity.SelectedIndex = 1;
    }
    whetherIsGood = true;
}

Code 3:

private void Page_Load(object sender, System.EventArgs e)
{
    //If you change the displayed city say from Dallas 
    //to Austin on the page and click "Set" button, 
    //it wrongly shows Dallas in the text box. 
    //But the variable correctly has a value of true.
    
    lstCity.SelectedIndex = 1;
    whetherIsGood = true;
}

As explained within the code comments, Code 2 has the expected behavior. So put the code which sets parameters to a server control within the if(!IsPostBack) code block, and put any code which sets a local variable outside this block, unless you are using a static variable.

Points of Interest

You could find a related article at MSDN.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

ThirstyMind



United States United States

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 4 PinmemberChannard6:18 12 Nov '10  
QuestionWhy? Pinmembernsimeonov12:01 1 Feb '06  
AnswerRe: Why? PinmemberThirstyMind12:27 1 Feb '06  
GeneralRe: Why? Pinmembernsimeonov12:42 1 Feb '06  
GeneralRe: Why? PinmemberThirstyMind12:52 1 Feb '06  
GeneralRe: Why? Pinmembernsimeonov12:58 1 Feb '06  
AnswerRe: Why? Pinmember.void.11:57 7 Sep '06  
GeneralRe: Why? Pinmembernsimeonov12:18 7 Sep '06  
GeneralRe: Why? Pinmember.void.4:14 8 Sep '06  
GeneralRe: Why? Pinmembernsimeonov4:29 8 Sep '06  
GeneralRe: Why? Pinmember.void.16:25 8 Sep '06  
It isn't about security, it is about page initialization and what actions you are supposed to perform - do you have to initialize some objects or they are already in tact. You may have <form action="SomeOtherPage.aspx"><input type="submit" value="click me"></form> and clicking the button will open "SomeOtherPage.aspx" and HttpMethod will indicate a POST not a GET, however IsPostBack wouldn't be true
I never said it was about security (though it is related, that's a different topic), I said you have no secure way to know which page you came from.
 
Back to topic, I suppose the way .Net knows if the page is a post back or not is if the controls are already loaded in memory or not (which seems to be a waste for me, but then again, I don't work for/with M$). The example you give me is interesting, I'll test it later, thank you.
 
The "hash" code you are referring is actually the hidden field __VIEWSTATE containing the values for all controls with property EnableViewState set to "true". So be careful (this is the default setting as well): If you want to be able to trigger "OnChange" event for textboxes or dropdownlists - leave it true, if not - you better set it to false and not waste bandwidth. DataGrids use ViewState heavily and add way too much overhead so if you can update the grid on every page load it would be better IMO.
Now, this is interesting. So, if I get you right this would be why some controls preserve a given status, as I asked before, right? Mhh... that deserves some tests (though they'll likely have to wait until Monday)
 
Oh man... oh man... How long did you work with asp.net, honestly, because what I read shows me that you don't understanding a single thing about how ASP.NET works and what it really is. This is one of the most ignorant and uniformed statements I've ever read recently... no joke. I know I wrote some stupid things on and off but saying that ASP.NET is somohow of an emulator of a desktop application for the web - now this deserves an award!
How much have I worked with Asp.Net? I thought I've already said I'm new to it, but I've been working on web projects some time now.
 
As for what is .Net, eto... a framework? That has nothing to do with it being a desktop application emulator for the web, since it can be both (and even a good one). I mean, if you understand that HTTP is an asynchronous/stateless client/server connection, then you do understand that preserving control states from one page to another (without me, the programmer, actually coding it) looks just like a desktop application emulation, right?. Anyway, that's just how I see Asp.Net thus far (I just asked someone who's been working with .Net for some years, and he agrees —although that weren't his exact words).
 
The award... is it an AlienWare? Maybe an iPod? I hope it's worth it!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 24 Jan 2006
Article Copyright 2006 by ThirstyMind
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid