简体   繁体   中英

Passing values between two “totally different” pages

To be clear, the two pages are only logically related in other words the values in my FirstPage.Aspx pass values to my SecondPage.Aspx so it control components and certain attributes of the controls in my SecondPage.Aspx, the SecondPage.Aspx is not a redirect or transfer from FirstPage.Asp. sorry am so sick of reading about all the answers but the one I actually need!

Consider the firstPage contains a textBox which get a value from the user or the web admin to set his preferences about the gridView page size on the SecondPage or a Text property of a label on the SecondPage

I want to explore my options to do so

PS: I was told to use a database to store values from the first page then on the page with my Label for example I connect to the database and retrieve the value and set it to the label...is this a best practice to let the page connect every time it loads to set some values sometimes it's just an int like 5, and most of the time I'd be connecting already to the database to display some table's data in a gridView or any databound control!

EDIT:

Sorry if I was a bit rude, it just hit me that I'm mainly getting the same answer that I read which is totally not my case!..and of course thanks to some of you mentioned the database solution, I hoped I could get an example or an article for my particular case since I think even it's simple but yet it's essential...

You can use:

  • Session variable
  • Cookie
  • Database value

each one of them have their own pros and cons

If you want the value to be stored the next time they come to page 2, use a database value. if not, use a session variable.

The database value will give you a persistent value, and you can then store other such user variables there.

the session will give persistent data, but only for the browsers current session. The data will be lost if their session times out.

From all you've stated, i would assume a database value.

ASP.NET framework provides several mechanisms to maintain context that lasts across several HTTP requests. The data you want accessible across calls, can be stored in this context. All you have to do is decide how long do you want this context to be maintained (because it consumes resources), and if you want this context to be available across more than one server.

  1. Application State : Which is maintained for the lifetime of the application ie from when the application is first loaded by ASP.NET, till it is unloaded for whatever reason.
  2. Session State : ASP.NET is able to identify a series of HTTP requests emanating from a specific client (IP address), close to each other in time, as a session. It can create a session context that persists across such a session, in which you can store data that is accessible to the calls in the session. The session state can be made available across server boundaries by associating it with a DB or shared memory.
  3. Database
  4. Viewstate : You can use Viewstate to maintain context, but keep in mind that Viewstate is transferred over the wire for every request / response. It has been known to get quite large, specially if you use controls.
  5. Cookies : Again, transferred on the wire for each request / response.

U can use cookies, session etc but if you want to pass something that defines the whole content of the page, like an ID of some sort, you might just want to put it in the QueryString. (ex: default.aspx?id=4)

Cons: everyone can read (or change) the value, not usable for critical data

Pros: everyone can read the value, and the link can be sent to others

I'd suggest you start here: Get Started with ASP.NET

If I understand your question, you are looking to do Cross Page Posting . If one page isn't posting to a 2nd, you'll need to persist the data somehow (database, session, xml file, etc). But, from reading your question, you sound like you just want to cross page post...

I think the better answer is "Control State"

Reference : http://www.asp.net/general/videos/how-do-i-use-control-state-to-persist-information-for-a-custom-web-server-control

but I couldn't get this implemented on my case, two different pages for example : the web user control is placed on News.aspx and the textbox which will have the value to be assigned to the control on News.aspx is placed in myAdmin.aspx page!

and to be honest, I didn't quite understand the code

So! do you think this is the best solution ? if yes do you think It can be used for my case (two different pages) ??

Your question is very confusing, took me a lot of time before I could I understand what you actually need. Since there is no events in HTML, you will need to use JavaScript and some kind of web-service (AJAX).

Page A will store the value in database, and JavaScript on page B will constantly check database for new values, get them and output to the user. In order to that it needs to connect to some webservice/webmethod that will return the value from database to your page. A good example can be found here .

Alternatively you can refresh page every 5 seconds using

<meta HTTP-EQUIV="Refresh" content="5">

and check for the new database value on Page_Load server event.

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