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:
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" ?
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:
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:
(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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.