ASP.NET Page Navigation
If you want to pass a variable content between your aspx web forms than you can you choose any one collection from ASP.NET page navigation techniques. Techniques are:
- QueryString
- Cookies
- Session
Query String
Query String is a value specified in an HTTP query that can be accessed easily within ASP.NET. The query string is appended at the end of the URL following the question mark ('?') character. Multiple query strings can be specified in the URL by separating them by either an ampersand('&') or a semicolon(';').
QueryString: The Request.QueryString collection is a NameValueCollection. The NameValueCollection is associated with the String keys and String values that can be accessed either with the key or with the index.
Example 1: Passing one Parameter
Response.Redirect("default2.aspx?name=Ankit"); Retrieve value on default2 page: Label1.Text = Request.QueryString["name"];
Or Label1.Text = Request["name"]
Example 2: Passing two Parameters
Response.Redirect("default2.aspx?name=Ankit&profession=Teacher");
Example 3: QueryString
using System.Collections.Specialized; protected void Page_Load(object sender, EventArgs e) { NameValueCollection n = Request.QueryString; if (n.HasKeys()) { string k = n.GetKey(0); string v = n.Get(0); Response.Write("Key is " + k + "Value is " + v); } } }
Count Query String Variables: Request.QueryString.Count.ToString();
Advantages of QueryString
- We can easily send value from one web page to another aspx page.
Disadvantages of QueryString
- QueryString have a max length, if you have to send a lot information this approach does not work.
- QueryString is visible in address bar of your browser so you should not use it with sensitive information. (No Security)
- QueryString can not be used to send &(ampersand) and space character.
Cookies
Cookie is a small text file sent by web server and saved by web browser on client machine. This allows web applications to save information for the user, and then re-use it on each page if needed.
Cookies may be used for authentication, identification of a user session, or anything else that can be accomplished through storing text data. Cookies can also be used for traveling of data from one page to another.
Cookies can be used as one of the following ways:
Way 1: (by using HttpCookies class)
HttpCookie StudentCookies = new HttpCookie("StudentCookies"); StudentCookies.Value = TextBox1.Text; StudentCookies.Expires = DateTime.Now.AddHours(1); Response.Cookies.Add(StudentCookies); Retrieve cookie value: string roll = Request.Cookies["StudentCookies"].Value;
Way 2 (by using Response directly)
Response.Cookies["StudentCookies"].Value = TextBox1.Text; Response.Cookies["StudentCookies"].Expires = DateTime.Now.AddDays(1); Retrieve cookie value: string roll = Request.Cookies["StudentCookies"].Value;
Way 3 (multiple values in same cookie)
Response.Cookies["StudentCookies"]["RollNumber"] = TextBox1.Text; Response.Cookies["StudentCookies"]["FirstName"] = "Abhimanyu"; Response.Cookies["StudentCookies"]["LastName"] = "Vatsa"; Response.Cookies["StudentCookies"]["TotalMarks"] = "450"; Response.Cookies["StudentCookies"].Expires = DateTime.Now.AddDays(1); Retrieve cookie value: string roll; roll = Request.Cookies["StudentCookies"]["RollNumber"]; roll = roll + " " + Request.Cookies["StudentCookies"]["FirstName"]; roll = roll + " " + Request.Cookies["StudentCookies"]["LastName"]; roll = roll + " " + Request.Cookies["StudentCookies"]["TotalMarks"]; Label1.Text = roll;
Limitations of Cookies
- Size of cookies is limited to 4096 bytes.
- Total 20 cookies can be used on a single website; if you exceed this browser will delete older cookies.
- End user can stop accepting cookies by browsers, so it is recommended to check the user's state and prompt the user to enable cookies.
Session State
ASP.NET allows you to save values using session state. The scope of Session state is the current browser session. If different users are using your application, each will have a different session state. In addition, if the same user leaves your application and then returns later, that user will also have a different session state. Session state is stored in the Session object as key-value pairs between server round trips.
A Session refers to the time duration during which a specific user is connected to a particular Web site. It begins when the user connects to the site and ends after user leaves the site. You can create a session object in the following way:
Session["UserName"] = "David"
You can retrieve the value of the session object in any page of your site, in the following way:
Response.Write( Session["UserName"] );
You can remove session objects from the session state by using the Remove or RemoveAll method.
Session.Remove["UserName"]; Session.RemoveAll
Session objects can store any kind of information and there is no restriction on the size of the data.