Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

When need "if not page.ispostback"? 1

Status
Not open for further replies.

adonet

MIS
May 4, 2004
312
US
Ispostback" drove me crazy!! Can some expert explain when need it?
 
In other words, when you only need to perform an action you on the first request (or for an action you only want to perform on subsequent requests). For example let's say you have a "Pets" DropDownList that you bind to a database table containing the values:

Dog
Cat
Parrot

by using:

ddlPets.DataSource = //get pets data from database
ddlPets.DataBind();

If the user, for example, clicks an "add to cart" button or something then the page/form will "post back". After some code to add an item to the cart, let's say you just want to send the user the original page with a message "Item Added To Cart" at the top.

Well, when you re-send the page, you don't need to get the pets information from the database a second time; You already have what you need! Because you don't want to waste time and resources making another trip to the database, you'd skip re-binding the control by putting the databinding code into a block that makes sure you only bind on the first request:

//if this isn't a "post back" i.e. this is the
//first request for the page, then bind the DropDownList
if( !Page.IsPostBack )
{
ddlPets.DataSource = //get data from database
ddlPets.DataBind();
}
//else, you don't need to make another trip to
//the database, so you don't re-bind


[COLOR=blue gainsboro]
Get a FREE iPod by helping me get mine! Click my referrer link:

More about the company and deal:
[/color]
 
Thank you. Now, my case is:
1) in Page_load, there is a dataGrid, grdOrder, need to bind;
2) in a button, after user input some parameters, the same datagrid, grdOrder, need to re-bind.
Question: Where need to add "if not page.ispostback" ?
 
So, most likely, "ispostback" only put in page_load?
 
Typically, though you'll see it in lesser used events like PreRender too.

You won't see the IsPostBack check in, for example, a button's event handler because in order to get there, the page must necessarily be posted back (in other words, the page must be created once before you can click the button).

[COLOR=blue gainsboro]
Get a FREE iPod by helping me get mine! Click my referrer link:

More about the company and deal:
[/color]
 
BoulderBum, can you explaim what difference in 3 cases. In page_load, I need load datagrid and dropdownlist:

1) If Not Page.IsPostBack Then
MyBindData()
FillDropDownList(Me.ddlOrderBy, strSQLOrderby, "Name")
End If
2)
MyBindData()
If Not Page.IsPostBack Then
FillDropDownList(Me.ddlOrderBy, strSQLOrderby, "Name")
End If
3) If Not Page.IsPostBack Then
FillDropDownList(Me.ddlOrderBy, strSQLOrderby, "Name")
End If
MyBindData()
 
Assuming MyBindData() binds the DataGrid: 1 binds the controls only once on the first page load, and the other two (2 & 3) end up binding the DataGrid every time (which is wasteful if the data never changes, but useful if MyBindData() relies on a value that changes in the page).

2 and 3 are probably equivalent unless a value from MyBindData() is used by FillDropDownList() and vice-versa.

[COLOR=blue gainsboro]
Get a FREE iPod by helping me get mine! Click my referrer link:

More about the company and deal:
[/color]
 
I used third party data grid from (Web GridEx)
It required load GridEx in page_load in "if not page.ispostback". So, I have to put FillDropDownList sub inside or outside block but doing so will make dropdownlist perform in very differen way: either take very long time to load (back to server) or text did not stay in display windows. I am still do not know why.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top