View State: Impart Technologies D. Harsha Vardhan
View State: Impart Technologies D. Harsha Vardhan
VARDHAN
D. HARSHA
View State
View State is a state that keeps the current state of the web page, during
post back.
Post Backing: The web page gets re-loaded automatically, whenever the
user performs an event at run time. Here, the page will be submitted to the
same page itself. This process is called as Post Backing. For example, if the
user clicks on a button, the page will be posted back.
At the time of post backing, the web page will be closed and the same web
page will be re-loaded. In fact, the textbox values / list box selections etc.,
would not be re-loaded, if View State concept is not implemented.
Because of View State, ASP.NET is able to get the web page to the previous
state, as it is submitted at the time of post back.
The query string is the URL, displayed in the browsers address bar.
When you want to pass one or more values from one page to another page as
arguments, you can send them with the Query String, in the following
format.
Syn:
http://localhost:portno/WebSiteName/WebPage.aspx?
parameter=value
Ex: http://localhost:portno/WebSiteName/WebPage.aspx?n=100
When you want to pass multiple values with the query string, use the
following format:
Syn: http://localhost:portno/WebSiteName/WebPage.aspx?
parameter1=value1& parameter2=value2
Hour 86 - Page 1 of 3
IMPART TECHNOLOGIES
VARDHAN
D. HARSHA
Ex: http://localhost:portno/WebSiteName/WebPage.aspx?
n1=140&n2=98
To get the parameter value in the next page, use this syntax:
Syn: Request.Params[parameter name]
Ex: Request.Params[n]
The above syntax gets the value of the given parameter, in string format.
Default.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
string name = TextBox1.Text;
Response.Redirect("display.aspx?arg=" + name);
}
display.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
string name = Request.Params["arg"];
Label1.Text = "Current User: " + name;
}
The values can be sent from one page to another page only. But, the values
cant be retrieved from other pages, forwarded from the second page.
You can share only string values or numerical values among multiple pages.
You cant share objects.
Hour 86 - Page 2 of 3
IMPART TECHNOLOGIES
VARDHAN
D. HARSHA
Cookies
A cookie will be created as a text file in the Temporary Internet Files folder
on the client system. This folder location depends on the browser type.
When compared with Query String, the advantage of cookies is, it doesnt
display the values in the address bar.
Another advantage of Cookies is, cookies alive among multiple web pages,
not only between one page to another page. That means you can access the
values stored in the cookies from any other pages, navigated from the current
page.
Implementation:
A. Assign value to the Cookie:
Default.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
string name = TextBox1.Text;
HttpCookie ck = new HttpCookie("username", name);
Response.Cookies.Add(ck);
Response.Redirect("display.aspx");
}
display.aspx.cs
3 of 3