简体   繁体   中英

How to share data between two web pages?

Im trying working on a web app project and trying to figure out how to display my answer on the second web page.

I have put aa text box on my first webpage and have corrected the coding of my application as I have received the correct answers in the textbox after I have debugged it.

Ideally I want to remove this textbox and want my answers which I managed to display on my textbox displayed on a label in the next webpage. Here is the calculation part of my code;

var cost = ((int)duration.TotalMinutes) * 0.35m;
                txtCost.Text = cost.ToString("c");

I'd like to make my answer appear in my second webpage and not have it displayed in the first. I have tried using Session["Cost"] = cost; on the button click event handler of the first webpage double cost = (double)(Session["Cost"]); lblDisplay.Text = cost.ToString("c"); double cost = (double)(Session["Cost"]); lblDisplay.Text = cost.ToString("c"); and this on the second webpage but every time I Debug it and run I always get $0.00 displayed on my label. Can someone help me fix this?

Sharing value between two views in MVC application, try following

// To save into the Cache    
System.Web.HttpContext.Current.Cache["CostKey"] = cost;

// To retrieve Cache Value
var cachedValue = System.Web.HttpContext.Current.Cache["CostKey"] as double;

For Session State, have a look at this link

In ASP.NET WebForms application, you can pass data around in various ways:

  1. Cache

    See the Learning Curve answer for examples. However, the object put in the cache is not guaranteed to be found again if the server experiences memory shortage or alike. The ASP.NET manages the cache and evicts objects on its own to maintain memory availability. This is in contrast with ApplicationState and SessionState where the objects are kept until they are removed manually, or the Application ends or Session expires.

  2. Session and Application states

    You can put any object in the SessionState object and retrieve it elsewhere in your code. However, you need to cast it appropriately as the SessionState accepts object -s. Eg if you store a number, when you retrieving it, you must do the casting yourself, just as you already did it. The reason it doesn't work, is perhaps you're trying to retrieve it from within another user's SessionState . Yes, the SessionState is a per-user structure. If you need to add the value as from one device and use it on another, use ApplicationState :

     Application["cost"] = cost; 
  3. Redirecting Response

    Using this technique, you could force the browser to request another page from the server and specify the full query string, including the variables you need. Eg :

     var destination = Server.UrlEncode("/someOtherPage.aspx?cost=34.65"); Response.Redirect(destination); 

    As an alternative, you can use Server.Transfer("someOtherPage.aspx") to save the roundtrip. However, in that case, the browser doesn't change the address in the address bar so the user is misled that she browses one page, but in fact, it is the someOtherPage.aspx .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM