Difference
between a Postback and a Callback in ASP.NET
A postback is
a request sent from a client to server from the same page, user is already
working with."
ASP.NET was introduced with a mechanism to
post an HTTP POST request back to the same page. It's basically
posting a complete page back to server (i.e. sending all of its data) on the
same page.So, the whole page is refreshed.
"A callback is generally a call for
execution of a function after another function has completed."
But if we try to
differentiate it from a postback, then we can say: It's a call made to the
server to ng AJAX, that makes a call to server and updating a part of the page
with specific data received.
Life
Cycle of an ASP.NET Page
First, you need to understand
what is the Life Cycle of an ASP.NET page?
When you open a web page in the browser, it
sends a request to the server and server processes this request and returns a
page to the browser. This complete process is known as Life cycle of a page.
How it starts: The life of an ASP.NET page starts when the
page is first requested by the user.
How it ends: It ends when the page is rendered completely to the browser.
How it ends: It ends when the page is rendered completely to the browser.
Until now, it's simple. On the server side,
the processing of an ASP.NET page is done in stages. At each stage, some events
are fired. With these events, you can write your own code to handle any
processing logic in ASP.NET page.
Now I will explain the different stages of
ASP.NET page life cycle. I have seen many explanations of the Page Life cycle
and none of them could make it into my favorite list. All the articles were
very lengthy and complex. I will try to explain it in the simplest language
with a clarity of thought.
Here are the list of stages:
I am writing this tip to explain the life cycle
of the page. So I try to simulate and experience the complete life cycle of an
ASP.NET page on my HP Notebook. What I will do? I will start my browser and
open an ASP.NET page. Then will tell you the complete story of the ASP.NET page
life cycle right from when I start typing the page URL in the browser to final
rendering of the page in my browser.
Let's start!
1. Browser makes a
request:
I open my browser Mozilla and type the URL of
an ASP.NET website. Let's say I typed http://woodland.com/. What does it means?
This means that I made a request to the browser to open this page for me.
Browser will send my request to the server on which this page is hosted.
2. Page framework
initialization: Page.Init event fires
ASP.NET checks whether the request is a new one
or an old one. If the request is a new one, then ASP.NET creates the page. It
generates the page with all the controls defined in the .aspx page.
If the page request is an old one, then
ASP.NET gets the data from View state and sets all the controls status View
State information and page is returned to the browser.
3. User Code
Initialization: Page.Load event fires.
In this event, initialization is done.
Populating the dynamic controls or dropdown list is done in this event. This
event always fires whether the page requested for the first time or page is
requested as part of a postback. Initialization is to be done only on the first
request. On a postback, you have to do nothing, ASP.NET restores the control
properties automatically from View State information.
4. Validation: After the page is loaded, validation controls
gets fired and displays error messages. You just have to check whether the Page.IsValid is true or false.
5. Event handling: Now the page is fully loaded and validated.
This stage includes any events that fired after the last postback. There are 2
types of events in an ASP.NET page life cycle:
1. Immediate Response events: For eg. Button
click, link click, etc. These events trigger a postback immediately.
2. Change Events: For example: Changing the
selection in a dropdown list or in a Textbox. These events fire when the page
is posted back next time.
6. Browser receives
response: Response and request
properties of the page are unloaded and any cleanup operation if required is
performed.
Example: I believe that there is nothing better than an
example to explain things. So I am providing you a page with a sample scenario.
I have created a page with a
Textbox and a submit button. I have written some text in Textbox and click
submit button triggering a Postback. Here is the list of events that fires in
this example:
1. Page.Init
2. Page.Load
3. Textbox.TextChanged
4. Button.Click
5. Page.PreRender
6. Page.Unload
Comments
Post a Comment