Click here to Skip to main content
Click here to Skip to main content

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

, 24 Jan 2006
Rate this:
Please Sign up or sign in to vote.
An article explaining IsPostBack checking in ASP.NET.

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

Share

About the Author

ThirstyMind

United States United States
No Biography provided

Comments and Discussions

 
QuestionISPost Back PinmemberMember 106951989-May-14 6:31 
GeneralMy vote of 4 PinmemberChannard12-Nov-10 5:18 
QuestionWhy? Pinmembernsimeonov1-Feb-06 11:01 
AnswerRe: Why? PinmemberThirstyMind1-Feb-06 11:27 
GeneralRe: Why? Pinmembernsimeonov1-Feb-06 11:42 
GeneralRe: Why? PinmemberThirstyMind1-Feb-06 11:52 
GeneralRe: Why? Pinmembernsimeonov1-Feb-06 11:58 
AnswerRe: Why? Pinmember.void.7-Sep-06 10:57 
GeneralRe: Why? Pinmembernsimeonov7-Sep-06 11:18 
GeneralRe: Why? Pinmember.void.8-Sep-06 3:14 

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.

| Advertise | Privacy | Mobile
Web02 | 2.8.140902.1 | Last Updated 24 Jan 2006
Article Copyright 2006 by ThirstyMind
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid