3.6 IsPostBack

The page exposes the IsPostBack property. This is a read-only Boolean property that indicates if the page or control is being loaded for the first time, or if it is being loaded in response to a client postback. Many expensive operations (such as getting data from a database or populating ListItems) must be performed only the first time the page or control is loaded. If the page is posted to the server and then reloaded, there is no need to repeat the operation. By testing the value of IsPostBack, you can skip the expensive operation, as in the code snippets in Example 3-1 and Example 3-2.

Example 3-1. Testing for IsPostBack in VB.NET
sub Page_Load(ByVal Sender as Object, _               ByVal e as EventArgs)    if not IsPostBack then       '  Do the expensive operations only the        '  first time the page is loaded.    end if end sub
Example 3-2. Testing for IsPostBack in C#
void Page_Load(Object sender, EventArgs e) {    if (! IsPostBack)    {       //  Do the expensive operations only the        //  first time the page is loaded.    } }


Programming ASP. NET
Programming ASP.NET 3.5
ISBN: 0596529562
EAN: 2147483647
Year: 2003
Pages: 156

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net