1

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.

0

8 Answers 8

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

5 Comments

Since the size of the string to be transferred is large, we cant use Querystring.
Dove, is there any other way?...Can we pass values if the 2 pages are not within the same class library?
how large? you can fit a lot in a querystring, otherwise temporarily put in session, cache or even in a cookie if that's allowed. depends on your setup
querystring has maxlength of 255 characters
fwiw querystrings have a max size closer to 8000 in practice, with some old IE installations at about 2000 but that's still not very much if you're trying to do something like pass a file. If you're putting that much in a querystring, you're doing it wrong though.
1

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"]]; 

3 Comments

For passing info from one page to another its better not to use Session.
Session objects are use to store session specific data and not page specific data.
Storing huge amounts of data in session is always a bad idea no matter what kind of session you use (inproc, state server, sql server)
1

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

Comments

0

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(); 

1 Comment

Thanks Himadri. But since the size of the string to be transferred is large, we cant use Querystring. Whats the other way?
0

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.

2 Comments

Thanks Eric. Can you be a bit more ellaborative on the session method?
See wef*s answer. I'd recommend looking at cross-page postbacks too.
0

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.

Comments

0

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.

Comments