Hi, We all know that one way of moving data between pages is to use the QueryString. however i don't like this method because is gives the user control over the data. I thought of using Session to store this data. for example: moving from a.aspx --> b.aspx
in a.aspx i have :
and then in b.aspx i retrieve the data for that page. What do you think? is this a good alternative to the QueryString? I'm concerned about the performce hit of putting too much data in the session bag. is there a way to do cleanning
like: Session["b.aspx"] = null; what are your thoughts on this.
daemon74, You can use session state to associate data with a user session and have it be available across pages - but as you said there is a certain performance and memory hit associated with that. If you are simply reluctant to expose information via querystring,
use the postback mechanism and store information in form fields/control viewstate to post data to another page - any postback enabled server control will let you do that (Buttons, HyperLinks, etc). This will let you send your information to another page via
the post request, which is hidden from the user. Of course, someone who really wanted to get/fabricate that information could inspect your generated html / send their own generated post data to your target page. Overall, if you wanted to completely protect
your information from the client, you would have to store it server side, in session (per user) or application/cache/database etc. If you just want to prevent people from having it readily available/changeable in the querystring, use the control state. Hope
this helps,
Mike Volodarsky
CTO at LeanSentry
Former IIS/ASP.NET PM
Want to become an expert at monitoring and troubleshooting your IIS applications?
See the demo at www.leansentry.com!
Thank you for your reply. i think your solution is much cleaner and is exactly what im looking for. the thing is i don't know how to use a control to transfer info from one page to another. an article or a small example will do.
Sure - check out some view state tutorials: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspnet/html/asp11222001.asp http://www.dotnetjohn.com/articles/articleid71.aspx In the simplest case, you can create a label control that will store
your information as you post from one page to another, and make it invisible (it wont render). If viewstate is enabled, you will be able to access the value of the textbox in the page accepting your postback. Of course, you can get fancier and write your own
server control that will provide strongly typed properties that will store the info in viewstate and allow you to pick them out after postback. The only limitation is, you can only have one page that each page posts to (determined in the <form runat="server">
element for your page). So, if you want to have a single page that can send information to multiple pages based on user input for example, you need to store the information in a session/application/file/database solution. Hope this helped - let us know if
you are having any issues
Mike Volodarsky
CTO at LeanSentry
Former IIS/ASP.NET PM
Want to become an expert at monitoring and troubleshooting your IIS applications?
See the demo at www.leansentry.com!
Thank you for the articles but they don't explain what i want. What i want to know is how to "transfer data from one page to another using Viewstate". to make things clear i'll show you how i use the session state to do that so that you can tell me how to replace
it with Viewstate. for example i have 2 pages a.aspx and b.aspx and i want to send data from a to b. then in a.aspx i have:
Page_Load(.....) {
Session["b"] = 13;
Response.Redirect("b.aspx");
}
Page_Load(......) {
if(Session["b"] == null) {
Response.Redirect("Error.aspx")
}
int i = (int) Session["b"];
// use i
}
This is using Session, now how is it done using Viewstate?
one way that i found for transfering data between pages without using querystring or session is to specify the action field in the html form tag. I found that while reading the code of IBuySpy Store and the way the do search. the way they're doing is by specify
the target page in the action tag like this:
<form method="post" id="frmSearch" name="frmSearch">
<input type="text" name="txtSearch" ID="txtSearch" SIZE="20">
<input type="image" src="images/arrowbutton.gif" border="0" id="image1" name="image1">
</form>
and in the Search result page they retrieve the value using Request.Param:
void Page_Load(Object sender, EventArgs e) {
// Search database using the supplied "txtSearch" parameter
IBuySpy.ProductsDB productCatalogue = new IBuySpy.ProductsDB();
MyList.DataSource = productCatalogue.SearchProductDescriptions(Request.Params["txtSearch"]);
MyList.DataBind();
// Display a message if no results are found
if (MyList.Items.Count == 0) {
ErrorMsg.Text = "No items matched your query.";
}
}
but then i don't really know how this can be usefull for me since most of the time i wan't to transfer data in the codebehide.
daemon74, Thanks for the clarification - I initially misunderstood what you were trying to do. While you wouldnt use viewstate to transfer your data from one page to another (only between postbacks to the same page), you can still use a hidden control to post
it to the target page. Due to the limitation I mentioned that prevents your server form from posting to pages other then the one where it is declared, you will have to create a separate html form to house your hidden input element that will contain your data.
However, you can still make that input element run at the server, and programatically set its value in Page_Load. Here is an example: ==PAGEA.ASPX=== <script runat="server">
</script> ============= While this does not expose the data to the user (like
querystring does), it can still be read if the user looks at the html source of the first page, and it can be provided by a malicious user by posting it to the second page. If you really want to avoid exposing the information, you should use a server-side
storage method. Alternatively, if you really dont want to or cant use server-side, you can use this mechanism along with encryption and signing to guarantee privacy and identity of the data. Hope this helps,
Mike Volodarsky
CTO at LeanSentry
Former IIS/ASP.NET PM
Want to become an expert at monitoring and troubleshooting your IIS applications?
See the demo at www.leansentry.com!
Yep it helps. thanks for your time. Although i don't think it's a good altervative to the querystring since it's much more complicated to implement specially when you firing events form datagrids or autopost back controls like dropdownlists. But it's still
good for use in special cases. Thanks for your input. I learnt a new technique.
No problem - keep in mind that you can still use the asp.net postback functionality for server controls such as ones you mention by placing them in the server form that posts back to the same page, even if you are using another form to send data to other pages.
Because of the way the postback model works in the first place, you wouldnt postback those control to another page, and so you are not really deviating from the development model if you are using the technique we talked about to send data to other pages. Best
of luck,
Mike Volodarsky
CTO at LeanSentry
Former IIS/ASP.NET PM
Want to become an expert at monitoring and troubleshooting your IIS applications?
See the demo at www.leansentry.com!
daemon74
Member
415 Points
83 Posts
Using Session to move data between pages
Sep 27, 2003 10:25 PM|LINK
Page_Load (.....) { Session["b.aspx"] = 1; Response.Redirect("b.aspx"); }and then in b.aspx i retrieve the data for that page. What do you think? is this a good alternative to the QueryString? I'm concerned about the performce hit of putting too much data in the session bag. is there a way to do cleanning like: Session["b.aspx"] = null; what are your thoughts on this.daemon74
Member
415 Points
83 Posts
Re: Using Session to move data between pages
Oct 01, 2003 06:15 AM|LINK
mvolo
Contributor
2202 Points
441 Posts
MVP
Re: Using Session to move data between pages
Oct 02, 2003 08:14 PM|LINK
CTO at LeanSentry
Former IIS/ASP.NET PM
Want to become an expert at monitoring and troubleshooting your IIS applications?
See the demo at www.leansentry.com!
daemon74
Member
415 Points
83 Posts
Re: Using Session to move data between pages
Oct 03, 2003 03:20 AM|LINK
mvolo
Contributor
2202 Points
441 Posts
MVP
Re: Using Session to move data between pages
Oct 03, 2003 04:04 AM|LINK
CTO at LeanSentry
Former IIS/ASP.NET PM
Want to become an expert at monitoring and troubleshooting your IIS applications?
See the demo at www.leansentry.com!
daemon74
Member
415 Points
83 Posts
Re: Using Session to move data between pages
Oct 04, 2003 01:01 AM|LINK
Page_Load(.....) { Session["b"] = 13; Response.Redirect("b.aspx"); } Page_Load(......) { if(Session["b"] == null) { Response.Redirect("Error.aspx") } int i = (int) Session["b"]; // use i }This is using Session, now how is it done using Viewstate?daemon74
Member
415 Points
83 Posts
Re: Using Session to move data between pages
Oct 04, 2003 01:38 AM|LINK
<form method="post" id="frmSearch" name="frmSearch"> <input type="text" name="txtSearch" ID="txtSearch" SIZE="20"> <input type="image" src="images/arrowbutton.gif" border="0" id="image1" name="image1"> </form> and in the Search result page they retrieve the value using Request.Param: void Page_Load(Object sender, EventArgs e) { // Search database using the supplied "txtSearch" parameter IBuySpy.ProductsDB productCatalogue = new IBuySpy.ProductsDB(); MyList.DataSource = productCatalogue.SearchProductDescriptions(Request.Params["txtSearch"]); MyList.DataBind(); // Display a message if no results are found if (MyList.Items.Count == 0) { ErrorMsg.Text = "No items matched your query."; } }but then i don't really know how this can be usefull for me since most of the time i wan't to transfer data in the codebehide.mvolo
Contributor
2202 Points
441 Posts
MVP
Re: Using Session to move data between pages
Oct 05, 2003 08:58 PM|LINK
void Page_Load(Object sender, EventArgs e) { data.Value="MY DATA"; }</script> <form method="post" action="PAGEB.aspx"> <input type="hidden" runat="Server" id="data" value="" /> <input type="submit" value="go to another page"> </form> <form runat="server" action="post"> </form> ============= ==PAGEB.ASPX=== <%@ Page language="c#" %> <script runat="server">void Page_Load(Object sender, EventArgs e) { data.Text=Request.Params["data"]; }</script> ============= While this does not expose the data to the user (like querystring does), it can still be read if the user looks at the html source of the first page, and it can be provided by a malicious user by posting it to the second page. If you really want to avoid exposing the information, you should use a server-side storage method. Alternatively, if you really dont want to or cant use server-side, you can use this mechanism along with encryption and signing to guarantee privacy and identity of the data. Hope this helps,CTO at LeanSentry
Former IIS/ASP.NET PM
Want to become an expert at monitoring and troubleshooting your IIS applications?
See the demo at www.leansentry.com!
daemon74
Member
415 Points
83 Posts
Re: Using Session to move data between pages
Oct 05, 2003 11:52 PM|LINK
mvolo
Contributor
2202 Points
441 Posts
MVP
Re: Using Session to move data between pages
Oct 06, 2003 01:43 AM|LINK
CTO at LeanSentry
Former IIS/ASP.NET PM
Want to become an expert at monitoring and troubleshooting your IIS applications?
See the demo at www.leansentry.com!