On the first page, I have a search form. I am trying to save all of the user's search parameters to the session, and I am displaying the user's search results on the second page using a control that pulls the values from the session. (I wanted the form
and the results to be on two different pages for "look and feel" purposes associated with my website)
The problem that I am having is that the first page isn't actually saving the values to the session. I have enabled sessions in my web config file, and I actually have this working fine on another page using different search parameters, but I can't figure
out what I am doing wrong here. Below is my "code behind" c# code. Anyone have any ideas as to what I am missing here? Thanks for your help!
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
Sometimes when you are setting a session before a redirection session values will be lost. The solution will be to end the response directly when using response.redirect
That is a good idea... unfortunately it didn't fix my problem. Maybe I have done something else wrong. I will go back and look at my aspx page code... or maybe the sql statement I am using on the results page is wrong. Thank you for your help!
Tell us: Are the keys stored in Session or you have session keys with empty values? I mean when you debug the code, can you step in FOR LOOP? If you are not able to step in, means session keys are not stored. And if then try to debug TextBoxes values, Are they
empty or they have values.
If they have values it means session is being lost in the Redirection. In that case Try Server.Transfer() instead of the Redirect().
Please let us know about the above questions.
Waiting to hear from you.
http://geekswithblogs.net/interfacemirror
Marked as answer by mary154458 on Aug 27, 2008 02:24 PM
Thanks so much for your help. I first tried stepping through like you said and I found that it was being lost in the redirect. Then, I saw that in the aspx page, I had indicated a post back url for the button. I removed that, and everything is working
fine now! I don't know why that should mess things up, since I used the right url in the post back url, but it works fine when that was removed. Thanks again!
The reason is very simple. When you set PostBackUrl of the button, it means that you want to use Cross-page postback. The mechanism used by ASP.NET to corss-page postback is Client Transfer not Server transfer.
Let me clear myself with following scenario:
Let say, you don't set PostbackUrl of the button and at the same time you are performing some operation in the Button_Click event. The event cycle would be like:
When you request the page, Page_Load is being called
When you press the button, AGAIN Page_Post is being called and after that Button_Clicked being called.
Soon after redirection Page_Load of the target page is being called.
This is because by default Button control posts back the page to the same container page.
Now for the second test you set PostbackUrl of the control, the event cycle is like:
When you request the page, Page_Load is being called
When you press the button, ONLY Page_Load of the target page is being called. This is because you have not post back the page to the same page (Even Button_Clicked is not being called). I have tested this few times.
The reason behind this PostBackUrl works on Client side, not on the server side. (it is completely different from Server.Transfer).
mary154458
Member
24 Points
35 Posts
Passing Session Variables from one page to another
Aug 26, 2008 02:23 PM|LINK
On the first page, I have a search form. I am trying to save all of the user's search parameters to the session, and I am displaying the user's search results on the second page using a control that pulls the values from the session. (I wanted the form and the results to be on two different pages for "look and feel" purposes associated with my website)
The problem that I am having is that the first page isn't actually saving the values to the session. I have enabled sessions in my web config file, and I actually have this working fine on another page using different search parameters, but I can't figure out what I am doing wrong here. Below is my "code behind" c# code. Anyone have any ideas as to what I am missing here? Thanks for your help!
using
System;using
System.Data;using
System.Configuration;using
System.Collections;using
System.Web;using
System.Web.Security;using
System.Web.UI;using
System.Web.UI.WebControls;using
System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;public
partial class Home : System.Web.UI.Page{
protected void Page_Load(object sender, EventArgs e){
}
protected void btnSearchAgents_Click(object sender, EventArgs e){
for (int x = Session.Count; x > 0; Session[--x] = null) ;Session.Add(
"SessionFirstName", txtFirstName.Text); Session.Add("SessionLastName", txtLastName.Text);Session.Add(
"SessionProvince", State.Text); Session.Add("SessionCounty", County.Text);Session.Add(
"SessionCity", City.Text); Session.Add("SessionLicType", LicenseTypeDropDown.Text);Session.Add(
"SessionLicStatus", LicenseStatusDropDown.Text); Response.Redirect("~/SearchResults-Agents.aspx");}
protected void Button2_Click(object sender, EventArgs e){
for (int x = Session.Count; x > 0; Session[--x] = null) ;Session.Add(
"SessionCompanyName", txtCompanyName.Text); Session.Add("SessionCompanyProvince", State1.Text);Session.Add(
"SessionCompanyCounty", County1.Text); Session.Add("SessionCompanyCity", City1.Text);Session.Add(
"SessionCompanyLicType", LicenseTypeDropDown1.Text); Session.Add("SessionCompanyLicStatus", LicenseStatusDropDown1.Text);Response.Redirect("~/SearchResults-Companies.aspx");}
protected void btnIndAdvancedSearch_Click (object sender, EventArgs e){
for (int x = Session.Count; x > 0; Session[--x] = null) ; Session.Add("SessionFirstName", txtFirstName.Text);Session.Add(
"SessionLastName", txtLastName.Text); Session.Add("SessionProvince", State.Text);Session.Add(
"SessionCounty", County.Text); Session.Add("SessionCity", City.Text);Session.Add(
"SessionLicType", LicenseTypeDropDown.Text); Session.Add("SessionLicStatus", LicenseStatusDropDown.Text);Response.Redirect("~/SearchResults-Agents.aspx");}
}
PSP_152890
Participant
783 Points
154 Posts
Re: Passing Session Variables from one page to another
Aug 26, 2008 03:50 PM|LINK
Change the redirect stmt. as like below
Response.Redirect("~/SearchResults-Companies.aspx",false);
Saravana Prakash P.
Haissam
All-Star
37421 Points
5632 Posts
Re: Passing Session Variables from one page to another
Aug 26, 2008 03:56 PM|LINK
Sometimes when you are setting a session before a redirection session values will be lost. The solution will be to end the response directly when using response.redirect
Response.Redirect("destinationPage.aspx",false);
Hope this helps
MCAD.NET
| Blog |
mary154458
Member
24 Points
35 Posts
Re: Passing Session Variables from one page to another
Aug 26, 2008 04:26 PM|LINK
That is a good idea... unfortunately it didn't fix my problem. Maybe I have done something else wrong. I will go back and look at my aspx page code... or maybe the sql statement I am using on the results page is wrong. Thank you for your help!
mary154458
Member
24 Points
35 Posts
Re: Passing Session Variables from one page to another
Aug 26, 2008 05:42 PM|LINK
I double checked my sql and also the aspx search page, but everything is right. Any other ideas?
Thanks,
Mary
InterfaceMir...
Member
147 Points
48 Posts
Re: Passing Session Variables from one page to another
Aug 27, 2008 12:43 PM|LINK
Hi Mary,
First try to narrow down your problem.
Tell us: Are the keys stored in Session or you have session keys with empty values? I mean when you debug the code, can you step in FOR LOOP? If you are not able to step in, means session keys are not stored. And if then try to debug TextBoxes values, Are they empty or they have values.
If they have values it means session is being lost in the Redirection. In that case Try Server.Transfer() instead of the Redirect().
Please let us know about the above questions.
Waiting to hear from you.
mary154458
Member
24 Points
35 Posts
Re: Passing Session Variables from one page to another
Aug 27, 2008 02:24 PM|LINK
Thanks so much for your help. I first tried stepping through like you said and I found that it was being lost in the redirect. Then, I saw that in the aspx page, I had indicated a post back url for the button. I removed that, and everything is working fine now! I don't know why that should mess things up, since I used the right url in the post back url, but it works fine when that was removed. Thanks again!
InterfaceMir...
Member
147 Points
48 Posts
Re: Passing Session Variables from one page to another
Aug 28, 2008 05:15 AM|LINK
Hi Mary,
The reason is very simple. When you set PostBackUrl of the button, it means that you want to use Cross-page postback. The mechanism used by ASP.NET to corss-page postback is Client Transfer not Server transfer.
Let me clear myself with following scenario:
Let say, you don't set PostbackUrl of the button and at the same time you are performing some operation in the Button_Click event. The event cycle would be like:
This is because by default Button control posts back the page to the same container page.
Now for the second test you set PostbackUrl of the control, the event cycle is like:
The reason behind this PostBackUrl works on Client side, not on the server side. (it is completely different from Server.Transfer).
For the reference you can see CROSS PAGE-POSTING IN ASP.NET WEB PAGES