Monday, June 1, 2009

Passing values to next page in asp.net

How to pass values between pages in asp.net?

There are several methods to pass values from one page to another page. Described below are few methods to pass values between pages:

1)querystring
2)context
3)session
4)application
5)view state

QueryString –
The QueryString collection is used to retrieve the variable values in the HTTP query string.. See the code below to see how QueryString functionality works.

The limit for the query string is 255 chars

//Code in home.aspx
String sString;
sString = Server.UrlEncode("string in home.aspx");
Response.Redirect("DestinationPage.aspx?Value=" & sString);

//Code in DestinationPage.aspx reads the QueryString
String sString;
sString = Request.QueryString("Value");
Response.Write("Your name is " & sString);



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.
‘Home.aspx stores value in context before sending it
Context.Items("MyData") = "beklo.com/rameshgoudd";
Server.Transfer("DestinationPage.aspx");

\\\'DestinationPage.aspx retrieves the value from home.aspx's context
String sString;
sString = Context.Items("MyDate").ToString;
Response.Write("The data is as follows: " & sString);
Session –When ever a client makes a request to the server it allocates a block of memory unique to the cient.

Session is stored in the server.

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.
‘home.aspx stores value in session before sending it

Session.Items("MyData") = "beklo.com/rameshgoudd";

Response.Redirect("DestinationPage.aspx");


In the page load event declare like this

\\\'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);

Application:

1. ASP.NET Framework applications consist of everything under one virtual directory of the Web server.
2. You create an ASP.NET Framework application by adding files to a virtual directory on the Web server.
3. The lifetime of an ASP.NET Framework application is marked by Application_Start and Application_End events.
4. Access to application-scope objects must be safe for multithreaded access.
Synatax of application:in the home.aspx
Application(“username”)=txtUsername.Text;
In the welcome.aspx calling that application
String s=application(“username”);

No comments:

Popular Posts