Next


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 don’t require to implement it.

Transferring Information between Pages With Query String

• The query string is the URL, displayed in the browser’s 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
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.

Demo on Query String with Parameters

viewstate

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 browser’s title bar.
• The values can be sent from one page to another page only. But, the values can’t be retrieved from other pages, forwarded from the second page.
• You can share only string values or numerical values among multiple pages. You can’t share objects.
Note: To overcome the above two limitations, “Cookies” concept is introduced.

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 doesn’t 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

Demo on Cookies

cookies

Default.aspx.cs
    protected void Button1_Click(object sender, EventArgs e)
    {
        string name =  user;
        HttpCookie ck = new HttpCookie("username", name);
        Response.Cookies.Add(ck);
        Response.Redirect("display.aspx");
    }
display.aspx.cs
    protected void Page_Load(object sender, EventArgs e)
    {
        string name = Request.Cookies["username"].Value;
        Label1.Text = "Current User: " + name;
    }