Solved

Page.Ispostback

Posted on 2006-07-17
9
399 Views
Last Modified: 2008-02-01
Morning all,

I have been a user of classic ASP for around 4 years and now I have ventured into .net.  I am doing OK but I am really struggling to understand what the page.ispostback is needed for?  Could someone give me a actual persons point of view because the text book answers I feel are too vague.

Thanks for your time,

Steve
0
Comment
Question by:stevemarsh99
9 Comments
 
LVL 23

Expert Comment

by:apresto
ID: 17120853
Hi stevemarsh99,

It tells you if the page is loaded for the first time or if it has been reloaded( posted to), for example, from a form

Apresto
0
 
LVL 23

Expert Comment

by:apresto
ID: 17120857
so if you had this:

if (page.isPostBack )
{
   Response.write "It has been opened before"
}
else
{
   Response.write "This is the first time"
}

you will see what its form, try sticking a link or a form with a submit button on the page and see the difference in results
0
 

Author Comment

by:stevemarsh99
ID: 17120858
But why is that of any value?
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 500 total points
ID: 17120862
Hi stevemarsh99,

The principle is this, when you *first* navigate to a page then IsPostback is false because of course no action has occured except that navigation. In classic ASP you would normally POST a form to another page or even back to the same page for further actions. With asp.net, any submit, button click or other autopostback event (such as setting a dropdownlist's autopostback property to true) will post the form back to the same page. When this occurs IsPostback is now True. This can be useful when you need to perform certain population routines or other actions on the first hit to a page, subsequent postbacks would not need to populate dropdownlists etc (providing you have not turned off viewstate for the control or page)  so do not need to be executed. IsPostback can be used to adjust the processing of the code appropriately.

Tim Cottee
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 23

Expert Comment

by:apresto
ID: 17120863
if you wanted to find out if a form was submitted or not, or if the page had been loaded via link
0
 

Author Comment

by:stevemarsh99
ID: 17120895
Sorry I still can't seem to get my head round it...all I can work out is that if it is false then it is the first time the page has been rendered....great.  If it is true then it has been sumbitted by something...again ... great...I just cant see what the benifits are?
0
 
LVL 23

Expert Comment

by:apresto
ID: 17120903
Well.

Lets say when the form is submitted you want to execute some data activity as you are submitting form information

If the page is loaded for the first time, you dont want to talk top the database as you wont have any form info as the page has been loaded for the first time, therefore, no form info has been filled out yet

0
 

Author Comment

by:stevemarsh99
ID: 17120905
Tim I just read through it realllllly slowly and it does now make sense!!! Thank you very much for your help guys....

:D

Steve
0
 
LVL 3

Expert Comment

by:harshits
ID: 17120950
Hi stevemarsh99,

"isPostBack" is very useful and something you cannot do without when you are using the asp.net databound controls.

These controls maintain the viewstate (i.e. for eg a selected value in the dropdownlist) to maintain the state of the control between postback's.

Now when you are binding the control to the datasource in the page_load event of the page, you need to make sure that it is written inside the if(!page.isPostBack) block.

Cause otherwise the control will keep re-binding to the data source after every postback and thus loose the viewstate value (the value which your user selected before clicking the submit button).

Harshit Sheth

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

4 Experts available now in Live!

Get 1:1 Help Now