There are several ways to accomplish the task to transfer data between pages. It can be a advantage to programmer to use these techniques according to their requirements other –wise selecting a inappropriate techniques in a situation may leads to wastage of time.
· Data Transfer Techniques are:
· Hidden Fields
· View State
· Session Variable
· Application Variables
1. Query String:
A query string is information that is appended to the end of a page URL. A query string is a collection of name/value pairs which are appended to a URL. They are separated from the location of the resource by a question mark ‘?’. QueryString method has the advantage of using simple, do not use server resources; drawback is that the value passed will be displayed in the browser’s address bar, the risk of being tampered with.
Passing the Value from Page A.aspx
Getting the value from Page B.aspx
string varB = Request.QueryString["varA"].ToString();
Cookie is a small text file sent by web server and saved by web browser on client machine. Cookies are created when a user's browser loads a particular website. The website sends information to the browser which then creates a text file. Every time the user goes back to the same website, the browser retrieves and sends this file to the website's server.
There are two types of cookies available in .Net
· Non Persistent / Session Cookies
· Persistent Cookies
Non Persistent / Session Cookies
Non Persistent cookies are stored in memory of the client browser session, when browsed is closed the non persistent cookies are lost.
Persistent Cookies have an expiration date and theses cookies are stored in Client hard drive.
Syntax:Creating Cookies and Storing Value in it.
//Creating Object for HttpCookie Class and Named it "UserInfo"
HttpCookie objCookie = new HttpCookie("UserInfo");
objCookie["UserName"] = TextBox1.Text.ToString();
objCookie["Password"] = TextBox2.Text.ToString();
//Specified time limit for cookie to accessed
objCookie.Expires = DateTime.Now.AddMinutes(30);
Getting Value from Cookie
HttpCookie objRequestRead = Request.Cookies["UserInfo"];
Response.Write(objRequestRead["UserName"] + " " + objRequestRead["password"]);
Cookies are stored for 24 hrs, if you have not specified any time limit for Cookies Expiration.
3. Hidden Fields
ASP.NET allows you to store information in a HiddenField control. A hidden field does not render visibly in the browser, but you can set its properties just as you can with a standard control. When a page is submitted to the server, the content of a hidden field is sent in the HTTP form collection along with the values of other controls. A hidden field acts as a repository for any page-specific information that you want to store directly in the page. Hidden fields' data is submitted to the server only in HTTP post operation. The value is saved as a string and therefore in order to use it for other types you need to perform casting.
//Creating and Assigning value to Hidden Filed
<asp:HiddenField ID="HiddenField1" runat="server" Value="0"/>
<asp:Button ID="Button1" runat="server" Text="Click" />
<asp:Label ID="Label1" runat="server" Text="Counter :"></asp:Label>