Querry String:

There are several methods to pass values from one page to another page. Described below are few methods to pass values between pages:QueryString - The QueryString method of passing values between web pages is one of the oldest methods of passing values between pages. A variable value is properly encoded before it is placed on a querystring. This is to make sure that characters that cause problems (like symbols and spaces) are encoded correctly. See the code below to see how QueryString functionality works.

//Code in InitialPage.aspxString sString;sString = Server.UrlEncode("string in InitialPage.aspx");Response.Redirect("DestinationPage.aspx?Value=" & sString);
//Code in DestinationPage.aspx reads the QueryStringString sString;sString = Request.QueryString("Value");Response.Write("Your name is " & sString);The data in the DestinationPage.aspx in the URL looks like this...


Context - The context object is used to send values between pages. Its similar to the session object, the difference being that, the Context object goes out of scope when the page is sent to a browser. Example code below shows how to use Context object.

'InitialPage.aspx stores value in context before sending it
Context.Items("MyData") = "initialvalue";
Server.Transfer("DestinationPage.aspx");'DestinationPage.aspx retrieves the value from InitialPage.aspx's contextString sString;sString = Context.Items("MyDate").ToString;Response.Write("The data is as follows: " & sString);

Session - The session object is used to persist data across a user session during the user's visit to a website. It is almost same as the Context object. When we use Response.Redirect, it causes the Context object to go away, so rather the Session object is used in such a scenario. Session object uses more of server memory than a context object. Example code below shows how to use Session object.

'InitialPage.aspx stores value in session before sending itSession.Items("MyData") = "initialvalue";

'DestinationPage.aspx retrieves the value from InitialPage.aspx's session

String sString;sString = Session.Items("MyDate").ToString;

Response.Write("The data is as follows: " & sString);
You may notice above, I have used Response.Redirect with session object, and server.transfer with a context object.

No comments: