ankitweblogic logo
W e b t u t o r i a l s

Website

ASP.NET Introduction

ASP.NET Web Forms

Standard Controls

Label Control

TextBox Control

Image Control

Button Controls

Redirecting User

Using HyperLinks

RadioButton Control

RadioButtonList

CheckBox Control

CheckBoxList Control

DropDownList

ListBox Control

ImageMap Control

Master Page

View State

Event-Driven Programming

IsPostBack

ASP.NET Page Structure

Visibility of Controls

Formatting Controls

Applying CSS

Dynamic CSS

Using Style Class

Themes and Skins

Panel Control

PlaceHolder Control

AdRotator Control

Calendar Control

Validation Controls

Page Navigation

User Control

Separating Code-Presentation

Overview of ADO.NET

Data Binding

DataBinding List Control

Page Navigation in ASP.NET

If you want to pass a variable content between your aspx webforms than you can you choose one collection from ASP.NET Page Navigation Techniques. Techniques are:
1. QueryString
2. Cookies
3. Session

1. A 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.

Passing one Parameter

Response.Redirect("default2.aspx?name=Ankit");

Passing two Parameters

Response.Redirect("default2.aspx?name=Ankit&lastname=Kumar");

Retrieve Value:

Label1.Text = Request.QueryString["NAME"];
Or Label1.Text = Request["NAME"]

Example:

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 this approach
It is very easy.

Disadvantages of this approach
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.
QueryString can not be used to send & and space characters.


2. Cookies: Cookie is a small text file sent by web server and saved by web browser on client machine. This allows webapplications 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 travelling of data from one page to another.

Cookies can be used as one of the following way:

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);
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);
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"]["MiddleName"] = "Kumar";
Response.Cookies["StudentCookies"]["LastName"] = "Vatsa";
Response.Cookies["StudentCookies"]["TotalMarks"] = "499";
Response.Cookies["StudentCookies"].Expires = DateTime.Now.AddDays(1);
string roll;
roll = Request.Cookies["StudentCookies"]["RollNumber"];
roll = roll + " " + Request.Cookies["StudentCookies"]["FirstName"];
roll = roll + " " + Request.Cookies["StudentCookies"]["MiddleName"];
roll = roll + " " + Request.Cookies["StudentCookies"]["LastName"];
roll = roll + " " + Request.Cookies["StudentCookies"]["TotalMarks"];
Label1.Text = roll;

Limitations of Cookies
1. Size of cookies is limited to 4096 bytes.
2. Total 20 cookies can be used on a single website; if you exceed this browser will delete older cookies.
3. 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.


3. Session State: ASP.NET allows you to save values using session state. The scope of Session state is to 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 the user visits on 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.