In this video
we will try to understand
what is PostBack?
How to detect a postback?
What is a need of Autopostback
what scenario we should be using postbacks
Postback occurs
when data is posted to a page
This data posting can happen when someone clicks on a submit button or some events occur on the UI controls
I have a very simple screen here
It has one button and a text box
PostBack will occur when I click on this button and I send data to the server
When a ASP.NET page is called or I will say when a ASP.NET page is loaded
There are lots of scenarios in which this page get loaded
For example somebody can just
put up the url of the page and try to load the page
Somebody probably can just hit the refresh button
or somebody can click on the submit button and send data In other words do a PostBack
you would like to detect if this page was called because of a PostBack or it was called because somebody just put the url on the browser
In order to detect if this page has been called because of the PostBack
We need to use something called as the IsPostBack property
The page object of ASP.NET has a property called as IsPostBack which becomes true
If the page is called because of PostBack
If you want to
use that property you have to say
this.IsPostBack
This IsPostBack property belongs to the page object which tells you that this
page was called because of PostBack or it was a just F5 refresh
It was called because somebody typed url on the browser
If I just go and put debug point here and if I click
on debug here or if I just start the application in a debug mode
Now there my application is running in a debug mode
In this scenario this page has been called you know from the browser
Its a first time this page is getting loaded
This page not called because somebody has send data to this page
This property should return to me
false because
this call is not a PostBack call
There my application is running
When I do a F5 it is not PostBack
I am not sending any kind of data
This value should be false
The IsPostBack property is false and that is proper
I type something in this text box and I click on the submit button
When I am clicking on the submit button actually I am sending data to the server
When this event happens I
expect that IsPostBack to be true. As I have defined previously
PostBack is nothing but you know send data to the server
When I click on this button
If I see the property
the value is
true
PostBack
tells you that
is there any kind of data which is send from the browser or not
Let's try to understand the other term
Autopostback
Add one more control here. I will add a checkbox
I already have a text box and a button
Let me add a check box
Let me run this application again in a debug mode
There my application is running
If I type something in this text box here
and if I tab out you can see the PostBack event is not happening
If I go and check and uncheck this check box again the PostBack event is not happening
Even typing on the text box
or checking and unchecking a check box
is also sending data to the server, the user want to send this data to the server
The PostBack event is not happening it is not going to the debug point
ASP.NET websites resides on central server
and they are connected by lot of users
You are changing a combo box you are checking a check box, unchecking a check box typing something on the text box
if you are going and make a trip to the server
then server can get superb loaded
By default this behavior is enable for ASP.NET
that means you have to fill the complete form and
once you are done with the
complete form filling you can then click on the submit button and then the data is send in one go
which leads to less consumption of bandwidth
there are less round trips
and the server also does not get loaded unnecessarily
There can be scenario where the user wants that basically as soon as he types on the text box the PostBack should happen
As soon as he clicks on this check box or he unchecks this check box the PostBack should happen
For that we need to go and set the Autopostback to true for each one of these controls
We need to go to these control
Go to the text box here and he will say Autopostback to true
Once you do Autopostback to true for control as soon as there is any kind of data change on that control the PostBack happens
Go to the check box here F4
that’s Autopostback
If I run this application
Click on this check box you can see as soon as he clicked on the check box the PostBack happened
This event is a PostBack
and then I can go and get the check box values
In the same way if I go and type on this text box here and if I do a tab
the PostBack is happening
Autopostback helps you to make the immediate PostBack. As soon as the data changes on the control
Postback is nothing but it happens when you send data to the server
If you want to detect this page has been loaded because of PostBack or probably is loaded because of refresh or not you can check the IsPostBack property of the page
Autopostback helps you to post as soon as the control data changes
I hope you enjoyed this video and
keep looking for ASP.NET videos on questpond.com. Thank you