Transfer variables between aspx pages

  • Thread starter Thread starter DJ Dev
  • Start date Start date
D

DJ Dev

This question must have been asked a lotta times. My question is that
how can I post variables from 1 aspx page to another. i dont wanna use
query strings.
Can I achieve this with Caching or Sessions or is there a custom
solution which can allow http posts?

Thanks for your help!
 
other than querystrings and sessions there is a third - most complicated way of transfering variables between pages..

you can transfer between pages using Server.Transfer() and then access the original request handler that did the transfer using Context.Handler and typecasting it..

Lets say you have a search page SearchPage with a textbox SearchPhraseTextBox that you input the search phrase into. Then you click a button Search. The form submits and the buttons onclick event is raised. In the onclick event handler you do Server.Transfer("SearchResults.aspx"); The server transfers to the SearchResults page(this is invisible to the user, it is a server transfer, not a response.redirect, the address in his browser will remain SearchPage.aspx). Within the SearchResults page you can access the original request handler - SearchPage like this: (SearchPage)Context.Handler
so you can get your search phrase like this

string searchPhrase = ((SearchPage)Context.Handler).SearchPhraseTextBox.Text

obviously you noticed we needed to typecast SearchPage on the Context.Handler, so obviously you must know on the results page what pages can transfer to it so it can type cast properly. You can do this by sending the original page in the querystring when doing server transfer like this Server.Transfer("SearchResults.aspx?originalHandler=SearchPage"); or you can check the type of Context.Handler or you can have all your pages that have a SearchPhraseTextBox inherit a class that inherits Page but contains a field called SearchPhrase and then you can type cast the inherited class safely on all pages that have the SearchPhraseTextBox for example.

This way of transfering variables is a bit nasty IMO and the adress in the browser window doesn't change when doing Server.Transfers which is not really cool...
 
One more way that Microsoft recommends against but uses themselves is hidden
form fields.

Dale

Adrijan Josic said:
other than querystrings and sessions there is a third - most complicated
way of transfering variables between pages...
you can transfer between pages using Server.Transfer() and then access the
original request handler that did the transfer using Context.Handler and
typecasting it...
Lets say you have a search page SearchPage with a textbox
SearchPhraseTextBox that you input the search phrase into. Then you click a
button Search. The form submits and the buttons onclick event is raised. In
the onclick event handler you do Server.Transfer("SearchResults.aspx"); The
server transfers to the SearchResults page(this is invisible to the user, it
is a server transfer, not a response.redirect, the address in his browser
will remain SearchPage.aspx). Within the SearchResults page you can access
the original request handler - SearchPage like this:
(SearchPage)Context.Handler.
so you can get your search phrase like this:

string searchPhrase = ((SearchPage)Context.Handler).SearchPhraseTextBox.Text;

obviously you noticed we needed to typecast SearchPage on the
Context.Handler, so obviously you must know on the results page what pages
can transfer to it so it can type cast properly. You can do this by sending
the original page in the querystring when doing server transfer like this
Server.Transfer("SearchResults.aspx?originalHandler=SearchPage"); or you can
check the type of Context.Handler or you can have all your pages that have a
SearchPhraseTextBox inherit a class that inherits Page but contains a field
called SearchPhrase and then you can type cast the inherited class safely on
all pages that have the SearchPhraseTextBox for example..
This way of transfering variables is a bit nasty IMO and the adress in the
browser window doesn't change when doing Server.Transfers which is not
really cool...
 
Back
Top