archana roy
archana roy

Reputation:

Pass values from 1 aspx page to another?

What are the different ways to transfer values from 1 code behind page to another? Can hidden fileds work without form tag? I have 2 code behind pages. I need to transfer a particular value from 1 page and fetch in the other code behind page. What is the best method which can be followed under this situation?

NB: Since the size of the string to be transferred is large, we cant use Querystring.

Upvotes: 1

Views: 3850

Answers (8)

Robert Paulson
Robert Paulson

Reputation: 18061

For truly large chunks of data, there's no way you want to send all that data to the client just to come right back. That's effectively what you're doing when you put a value as a form field or a querystring. You'll use up bandwidth and slow down your users experience.

Storing huge amounts of data in the session is a very bad idea. It's either in-proc which is using up your servers main memory, or it's being serialized to another server (State Server), or being stored in a database (Sql State). Session data is retrieved for each and every request. This is a total performance killer.

If you really need to pass that much data from one page to the next, evaluate your consistency needs. If it's really transactional, perhaps you need to bite the bullet and store in a database. The other page can retrieve if necessary.

Most databases can (effectively) store any amount of data you throw at them, but don't abuse the database if you don't need the consistency a database gives you. Off the top of my head, I'd go for around 8k of data. I recognize varchar(max), etc, but you need to evaluate the tradeoff of storing all that (transient) data, log space, etc.

Otherwise, there's nothing wrong with creating a temporary file on a shared network disk and passing a token around. Use a guid and the date to generate a unique filename.

Upvotes: 0

Pranali Desai
Pranali Desai

Reputation: 974

Use Session that would do the job for you but since Session should contain session specific data you have to use it properly you can also use Server.Transfer

Upvotes: 1

rahul
rahul

Reputation: 187020

For transferring the control from one page to another you can use

Server.Transfer ( "newpage.aspx" );

Method

and for passing value from one page to another you can use Context

Context.Items["Value1"] = "value to pass";

and retrieve the value in the target page as

string val = Context.Items["Value1"].ToString(); // check for null

The maximum length of querystring will be different in different browsers.

Also passing sensitive information through querystring is not a good approach.

Response.Redirect involves a roundtrip to the server whereas Server.Transfer conserves server resources by avoiding the roundtrip. It just changes the focus of the webserver to a different page and transfers the page processing to a different page.

If you are using Server.Transfer then you can directly access the values, controls and properties of the previous page which you can’t do with Response.Redirect.

Response.Redirect changes the URL in the browser’s address bar. So they can be bookmarked. Whereas Server.Transfer retains the original URL in the browser’s address bar. It just replaces the contents of the previous page with the new one.

Response.Redirect can be used for both .aspx and html pages whereas Server.Transfer can be used only for .aspx pages and is specific to ASP and ASP.NET.

Response.Redirect can be used to redirect a user to an external websites. Server.Transfer can be used only on sites running on the same server. You cannot use Server.Transfer to redirect the user to a page running on a different server.

Upvotes: 0

wefwfwefwe
wefwfwefwe

Reputation: 3518

First Page

//create a unique key
string guid = Guid.NewGuid().ToString();
//store large data in the session
Session[guid] = BIG_VALUE_TO_TRANSFER;
//redirect to new page, passing key as parameter
Response.Redirect("SecondPage.aspx?guid="+guid");

Second Page

//retrieve data from session using the key in the querystring
//you should really validate this but i can't be bothered
BIG_TYPE bigvalue = (BIG_TYPE)Session[Request.QueryString["guid"]];

Upvotes: 1

Pete
Pete

Reputation: 12553

You could use cross-page posting. When using that, you simply specify that a different page should handle the postback, see this link: http://msdn.microsoft.com/en-us/library/ms178139.aspx

I generally would not recommend using session variables, as they reduce the scalability of the application, and they don't work well with browser-back button

edit: Another option could be to store the data in the current HttpContext and then use Server.Transfer

HttpContext.Current.Items["tempData"] = yourLongData;
Server.Transfer("NewPage.aspx");

On the new page, you can read the value from the HttpContext. This would work because the the new page is processed in the same request, and therefore the same context.

But that doesn't cause a client redirect, so it may not be applicable in every case

Upvotes: 3

Per Erik Stendahl
Per Erik Stendahl

Reputation: 883

You can store the value in Session. If concurrency is an issue you can use a unique key (like a guid) and pass the key in the querystring.

Upvotes: 0

Himadri
Himadri

Reputation: 8876

Hidden fields cannot work without form tag.

a) You can post the page putting the particular value in a hiddenfield or text field and retrieve it in next page by string MyVal = Request.Form["FieldName"];

b) you can send the value by querystring. Suppose u r redirecting to default2.aspx with val=6 e.g. Response.Redirect("default2.aspx?val=6"); and retrieve it in next page by

string MyVal = Request.QueryString["val"].ToString();

Upvotes: 0

dove
dove

Reputation: 20674

Querystring it would the winner.

Though if it is the code you are trying to call from one page to another then it sounds like you might have trouble. The code you are calling from both pages should be in a class library, but if that's the case, this answer would be very long and some might say the question too open.

Upvotes: 1

Related Questions