Está en la página 1de 3

IMPART TECHNOLOGIES

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.

As a part of this View State, at the time of Post Backing, ASP.NET


automatically saves the current values of the controls in a separate string
temporarily. After re-loading the page, the controls values will be re-stored to
its previous state, based on the values in view state string.

In fact, the View State is maintained automatically by ASP.NET, you dont


require to implement it.

Transferring Information between Pages


With Query String

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

.NET 3.5 and Visual Studio 2008

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.

Application 150: Demo on Query String with


Parameters

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;
}

Limitations of Passing Values with Query String:

No security is available, because the argument names and values will be


displayed in the browsers title bar.

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.

Note: To overcome the above two limitations, Cookies concept is introduced.

.NET 3.5 and Visual Studio 2008

Hour 86 - Page 2 of 3

IMPART TECHNOLOGIES
VARDHAN

D. HARSHA

Cookies

A cookie can be used to share values among multiple web pages.

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:

Create cookie object:


HttpCookie obj = new HttpCookie(name, value);

Assign the cookie object to Response:


Response.Cookies.Add(obj);

B. Get value from the Cookie:


Request.Cookies[name].Value

Application 151: Demo on


Cookies

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

.NET 3.5 and

protected void Page_Load(object sender, EventArgs e)


{
name
= Request.Cookies["username"].Value;
Visual string
Studio
2008
Hour 86 - Page
Label1.Text = "Current User: " + name;
}

3 of 3