Lecture 3
Lecture 3
NET Course
by T. Ala’a Khalid
1
Lecture 3
ViewState in ASP.Net
2
Lecture Objectives
Stateless nature of HTTP protocol
How a web form is processed
What is ViewState
The role of ViewState in ASP.Net
ASP.Net server controls & ViewState
Primary difference between ASP.Net server
controls and HTML Controls
3
Stateless nature of HTTP Protocol
4
How a web form is processed
5
ViewState
6
The role of ViewState in ASP.Net
Click the button now, and the value gets
incremented every time we click. So how is this
possible now?
It is possible because, we are using the ViewState
variable “Clicks” to preserve the data between
requests
The ViewState data travels with every request and
response between the client and the web server
ViewState variables are used to preserve data
across page postbacks
7
ASP.Net server controls & ViewState
8
ASP.Net server controls & ViewState
Upon clicking the button, the value gets incremented
correctly as expected
This is possible because, TextBox1 is an asp.net server
control, that uses ViewState internally to preserve data
across postbacks
Because web forms has very short lifetimes, ASP.Net
takes special steps to preserve the data entered in the
controls on a web form
Data entered in controls is sent with each request and
restored to controls in Page_Init()
The data in these controls is then available in the
Page_Load(), Button_Click(), and many more events that
occur after Page_Init() event
9
ASP.NET server controls & HTML controls
The Primary difference is that
ASP.NET server controls retains state
HTML controls do not retain state across postbacks
10
ASP.NET server controls & HTML controls
An HTML control can be converted in ASP.NET server
controls, by adding runat=“server” attribute in the HTML
source as shown below
11