Session variable value changes between page processing somehow

vonke999

New Member
I have a script that processes a search query. The results are returned with a sortable header, so clicking on the Lot # column title would sort the results by the lot # ASC. However, if you had already sorted it ASC (which this is stored in a session variable), it will change it to sort it by the lot # DESC (and update that session variable).All of this works perfectly, so long as thumbnails are not included in the search results. When I do include them (which all that does is change the search query to include the image path, then output an image for the image column), it all breaks. Here is what happens:When the page loads, I \[code\]response.write(Session["sort"])\[/code\] to see how I am sorting (ASC or DESC).If a column header is clicked, I check to see if the new "order by" column was already the "order by" column (also stored in a session variable, \[code\]Session["orderby"]\[/code\]).If it was not previously set as the order by variable, it sets the sort session variable to ASC and sets the order by session variable to the column name.If it was previously set as the order by variable, it checks to see what the current sort session variable equals. If it equals ASC, then it sets the sort session variable to DESC and the order by session variable stays the same. If it equals DESC, it sets the sort session variable to ASC and the order by variable remains the same.This part actually still works to. The problem is that at the end of processing the page, I again \[code\]response.write(Session["sort"])\[/code\] to see what the variable has been set to. This is the very last thing that happens before the page loads to the screen. So remember, the very first thing that happens when the page is hit is it prints out the sort session variable, and the very last thing it does is prints out the variable again.So, when images are included in the page, clicking one column will cause it to sort either ASC or DESC (depending on which column I click on, as it seems to alternate with every-other column). So lets say the column I clicked caused the first \[code\]response.write(Session["sorty"])\[/code\] to be blank (since it's not set yet), and the second to print ASC. When I click the column again, the first will print DESC (which should be ASC since absolutely nothing is changed between the last one printed and the first one printed), and the second again prints ASC.So somehow, the session variable is being changed between page loads when there is absolutely no page processing between the last print and the first print. (And on every-other column, it's reversed, showing up ASC at the top, and DESC at the bottom every single time.) Again, this works perfectly without the images.Here's the only code with the thumbnail include script in it:\[code\] if (Session["incThumb"] != null) { resultText += "<table style=\"width:100%;\">\n<tr style=\"font-weight:bold;text-align:left;\">\n<th style=\"border-bottom:1px solid #000;\">Thumb</th>\n<th style=\"border-bottom:1px solid #000;\"><a style=\"font-weight:bold; font-color:#000000;\" href=http://stackoverflow.com/"searchResults.aspx?page=" + page + "&orderBy=tblItem.item_id\">Item ID</a></th>\n<th style=\"border-bottom:1px solid #000;\"><a style=\"font-weight:bold; font-color:#000000;\" href=http://stackoverflow.com/"searchResults.aspx?page=" + page + "&orderBy=item_title\">Title</a></th>\n<th style=\"border-bottom:1px solid #000;\"><a style=\"font-weight:bold; font-color:#000000;\" href=http://stackoverflow.com/"searchResults.aspx?page=" + page + "&orderBy=tblItem.item_lot\">Lot</a></th>\n<th style=\"border-bottom:1px solid #000;\"><a style=\"font-weight:bold; font-color:#000000;\" href=http://stackoverflow.com/"searchResults.aspx?page=" + page + "&orderBy=tblItem.item_cs_txt_id\">Consignor ID</a></th>\n<th style=\"border-bottom:1px solid #000;\"><a style=\"font-weight:bold; font-color:#000000;\" href=http://stackoverflow.com/"searchResults.aspx?page=" + page + "&orderBy=tblMcat.mcat_name\">Master Category</a></th>\n<th style=\"border-bottom:1px solid #000;\"><a style=\"font-weight:bold; font-color:#000000;\" href=http://stackoverflow.com/"searchResults.aspx?page=" + page + "&orderBy=tblCat.cat_name\">Category</a></th>\n<th style=\"border-bottom:1px solid #000;\"><a style=\"font-weight:bold; font-color:#000000;\" href=http://stackoverflow.com/"searchResults.aspx?page=" + page + "&orderBy=tblItem.item_est_lo\">Low Est.</a></th>\n<th style=\"border-bottom:1px solid #000;\"><a style=\"font-weight:bold; font-color:#000000;\" href=http://stackoverflow.com/"searchResults.aspx?page=" + page + "&orderBy=tblItem.item_est_hi\">High Est.</a></th>\n<th style=\"border-bottom:1px solid #000;\"><a style=\"font-weight:bold; font-color:#000000;\" href=http://stackoverflow.com/"searchResults.aspx?page=" + page + "&orderBy=tblItem.item_reserve\">Reserve</a></th>\n<th style=\"border-bottom:1px solid #000;\"><a style=\"font-weight:bold; font-color:#000000;\" href=http://stackoverflow.com/"searchResults.aspx?page=" + page + "&orderBy=tblItem.item_status\">Status</a></th>\n<th style=\"border-bottom:1px solid #000;\"><a style=\"font-weight:bold; font-color:#000000;\" href=http://stackoverflow.com/"searchResults.aspx?page=" + page + "&orderBy=tblItem.item_pr\">Hammer Price</a></th>\n<th style=\"border-bottom:1px solid #000;\"><a style=\"font-weight:bold; font-color:#000000;\" href=http://stackoverflow.com/"searchResults.aspx?page=" + page + "&orderBy=tblMailList.mail_ID\">Bidder ID</a></th>\n</tr>\n"; } else { resultText += "<table style=\"width:100%;\">\n<tr style=\"font-weight:bold;text-align:left;\">\n<th style=\"border-bottom:1px solid #000;\"><a style=\"font-weight:bold; font-color:#000000;\" href=http://stackoverflow.com/"searchResults.aspx?page=" + page + "&orderBy=tblItem.item_id\">Item ID</a></th>\n<th style=\"border-bottom:1px solid #000;\"><a style=\"font-weight:bold; font-color:#000000;\" href=http://stackoverflow.com/"searchResults.aspx?page=" + page + "&orderBy=item_title\">Title</a></th>\n<th style=\"border-bottom:1px solid #000;\"><a style=\"font-weight:bold; font-color:#000000;\" href=http://stackoverflow.com/"searchResults.aspx?page=" + page + "&orderBy=tblItem.item_lot\">Lot</a></th>\n<th style=\"border-bottom:1px solid #000;\"><a style=\"font-weight:bold; font-color:#000000;\" href=http://stackoverflow.com/"searchResults.aspx?page=" + page + "&orderBy=tblItem.item_cs_txt_id\">Consignor ID</a></th>\n<th style=\"border-bottom:1px solid #000;\"><a style=\"font-weight:bold; font-color:#000000;\" href=http://stackoverflow.com/"searchResults.aspx?page=" + page + "&orderBy=tblMcat.mcat_name\">Master Category</a></th>\n<th style=\"border-bottom:1px solid #000;\"><a style=\"font-weight:bold; font-color:#000000;\" href=http://stackoverflow.com/"searchResults.aspx?page=" + page + "&orderBy=tblCat.cat_name\">Category</a></th>\n<th style=\"border-bottom:1px solid #000;\"><a style=\"font-weight:bold; font-color:#000000;\" href=http://stackoverflow.com/"searchResults.aspx?page=" + page + "&orderBy=tblItem.item_est_lo\">Low Est.</a></th>\n<th style=\"border-bottom:1px solid #000;\"><a style=\"font-weight:bold; font-color:#000000;\" href=http://stackoverflow.com/"searchResults.aspx?page=" + page + "&orderBy=tblItem.item_est_hi\">High Est.</a></th>\n<th style=\"border-bottom:1px solid #000;\"><a style=\"font-weight:bold; font-color:#000000;\" href=http://stackoverflow.com/"searchResults.aspx?page=" + page + "&orderBy=tblItem.item_reserve\">Reserve</a></th>\n<th style=\"border-bottom:1px solid #000;\"><a style=\"font-weight:bold; font-color:#000000;\" href=http://stackoverflow.com/"searchResults.aspx?page=" + page + "&orderBy=tblItem.item_status\">Status</a></th>\n<th style=\"border-bottom:1px solid #000;\"><a style=\"font-weight:bold; font-color:#000000;\" href=http://stackoverflow.com/"searchResults.aspx?page=" + page + "&orderBy=tblItem.item_pr\">Hammer Price</a></th>\n<th style=\"border-bottom:1px solid #000;\"><a style=\"font-weight:bold; font-color:#000000;\" href=http://stackoverflow.com/"searchResults.aspx?page=" + page + "&orderBy=tblMailList.mail_ID\">Bidder ID</a></th>\n</tr>\n"; }\[/code\]Then just below this, I also account for it when printing out the SQL results, which is (in English), if the session variable for including thumbnails is not null, include the extra table cell for thumbnails and add in the image.Needless to say, this one has me stumped. I would appreciate any and all responses/comments. Let me know if you want to see anymore code.EDITHere's the response.write code:\[code\]protected void Page_Load(object sender, EventArgs e){ Response.Write(Session["sort"] + " 1<br/>"); string orderBy = ""; base.SubNavItems.Add("Search Home", "searchHome.aspx"); if (!IsPostBack) { } if (!String.IsNullOrEmpty(Request.QueryString["page"])) { if (!String.IsNullOrEmpty(Request.QueryString["orderBy"])){ orderBy = Request.QueryString["orderBy"].ToString(); } get_page(Int32.Parse(Request.QueryString["page"].ToString()), orderBy); } if (!String.IsNullOrEmpty(Request.Form["ctl00$ContentPlaceHolder1$save"])) { click_search(); } Response.Write(Session["sort"] + " 1<br/>");}\[/code\]So because page is always set in the column headers, the \[code\]get_page()\[/code\] method is called when I sort a column. \[code\]orderBy\[/code\] is set through the query string and passed along to the method \[code\]protected void get_page(Int32 page, String orderBy)\[/code\] which is what contains the below code. At the end of that method is also a \[code\]Response.Write(Session["sort"] + " 2<br/>");\[/code\] line which is the last thing that method does. No other method runs at this point. Also, I just printed out the sort variable from the .aspx page and got the same result as the last print out of the variable that the .aspx.cs page prints (which is then different from the first one printed the next time).I figure I'll go ahead and add the logic to the whole order by/sort:\[code\] if (Session["orderBy"] != null) { if (Session["orderBy"].ToString() == orderBy) { if (Session["sort"].ToString() == "ASC") { Session["sort"] = "DESC"; } else { Session["sort"] = "ASC"; } } else if (!String.IsNullOrEmpty(orderBy)) { Session["sort"] = "ASC"; Session["orderBy"] = orderBy; } } else if (!String.IsNullOrEmpty(orderBy)) { Session["sort"] = "ASC"; Session["orderBy"] = orderBy; } else { Session["sort"] = "ASC"; Session["orderBy"] = "tblItem.item_id"; }\[/code\]
 
Back
Top