title: Session variable value changes between page processing somehow
body: <p>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).</p>
  
  <p>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:</p>
  
  <p>When the page loads, I <code>response.write(Session["sort"])</code> to see how I am sorting (ASC or DESC).</p>
  
  <p>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>).</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Here's the only code with the thumbnail include script in it:</p>
  
  <pre><code>    if (Session["incThumb"] != null)
      {
          resultText += "&lt;table style=\"width:100%;\"&gt;\n&lt;tr style=\"font-weight:bold;text-align:left;\"&gt;\n&lt;th style=\"border-bottom:1px solid #000;\"&gt;Thumb&lt;/th&gt;\n&lt;th style=\"border-bottom:1px solid #000;\"&gt;&lt;a style=\"font-weight:bold; font-color:#000000;\" href=\"searchResults.aspx?page=" + page + "&amp;orderBy=tblItem.item_id\"&gt;Item ID&lt;/a&gt;&lt;/th&gt;\n&lt;th style=\"border-bottom:1px solid #000;\"&gt;&lt;a style=\"font-weight:bold; font-color:#000000;\" href=\"searchResults.aspx?page=" + page + "&amp;orderBy=item_title\"&gt;Title&lt;/a&gt;&lt;/th&gt;\n&lt;th style=\"border-bottom:1px solid #000;\"&gt;&lt;a style=\"font-weight:bold; font-color:#000000;\" href=\"searchResults.aspx?page=" + page + "&amp;orderBy=tblItem.item_lot\"&gt;Lot&lt;/a&gt;&lt;/th&gt;\n&lt;th style=\"border-bottom:1px solid #000;\"&gt;&lt;a style=\"font-weight:bold; font-color:#000000;\" href=\"searchResults.aspx?page=" + page + "&amp;orderBy=tblItem.item_cs_txt_id\"&gt;Consignor ID&lt;/a&gt;&lt;/th&gt;\n&lt;th style=\"border-bottom:1px solid #000;\"&gt;&lt;a style=\"font-weight:bold; font-color:#000000;\" href=\"searchResults.aspx?page=" + page + "&amp;orderBy=tblMcat.mcat_name\"&gt;Master Category&lt;/a&gt;&lt;/th&gt;\n&lt;th style=\"border-bottom:1px solid #000;\"&gt;&lt;a style=\"font-weight:bold; font-color:#000000;\" href=\"searchResults.aspx?page=" + page + "&amp;orderBy=tblCat.cat_name\"&gt;Category&lt;/a&gt;&lt;/th&gt;\n&lt;th style=\"border-bottom:1px solid #000;\"&gt;&lt;a style=\"font-weight:bold; font-color:#000000;\" href=\"searchResults.aspx?page=" + page + "&amp;orderBy=tblItem.item_est_lo\"&gt;Low Est.&lt;/a&gt;&lt;/th&gt;\n&lt;th style=\"border-bottom:1px solid #000;\"&gt;&lt;a style=\"font-weight:bold; font-color:#000000;\" href=\"searchResults.aspx?page=" + page + "&amp;orderBy=tblItem.item_est_hi\"&gt;High Est.&lt;/a&gt;&lt;/th&gt;\n&lt;th style=\"border-bottom:1px solid #000;\"&gt;&lt;a style=\"font-weight:bold; font-color:#000000;\" href=\"searchResults.aspx?page=" + page + "&amp;orderBy=tblItem.item_reserve\"&gt;Reserve&lt;/a&gt;&lt;/th&gt;\n&lt;th style=\"border-bottom:1px solid #000;\"&gt;&lt;a style=\"font-weight:bold; font-color:#000000;\" href=\"searchResults.aspx?page=" + page + "&amp;orderBy=tblItem.item_status\"&gt;Status&lt;/a&gt;&lt;/th&gt;\n&lt;th style=\"border-bottom:1px solid #000;\"&gt;&lt;a style=\"font-weight:bold; font-color:#000000;\" href=\"searchResults.aspx?page=" + page + "&amp;orderBy=tblItem.item_pr\"&gt;Hammer Price&lt;/a&gt;&lt;/th&gt;\n&lt;th style=\"border-bottom:1px solid #000;\"&gt;&lt;a style=\"font-weight:bold; font-color:#000000;\" href=\"searchResults.aspx?page=" + page + "&amp;orderBy=tblMailList.mail_ID\"&gt;Bidder ID&lt;/a&gt;&lt;/th&gt;\n&lt;/tr&gt;\n";
      }
      else
      {
          resultText += "&lt;table style=\"width:100%;\"&gt;\n&lt;tr style=\"font-weight:bold;text-align:left;\"&gt;\n&lt;th style=\"border-bottom:1px solid #000;\"&gt;&lt;a style=\"font-weight:bold; font-color:#000000;\" href=\"searchResults.aspx?page=" + page + "&amp;orderBy=tblItem.item_id\"&gt;Item ID&lt;/a&gt;&lt;/th&gt;\n&lt;th style=\"border-bottom:1px solid #000;\"&gt;&lt;a style=\"font-weight:bold; font-color:#000000;\" href=\"searchResults.aspx?page=" + page + "&amp;orderBy=item_title\"&gt;Title&lt;/a&gt;&lt;/th&gt;\n&lt;th style=\"border-bottom:1px solid #000;\"&gt;&lt;a style=\"font-weight:bold; font-color:#000000;\" href=\"searchResults.aspx?page=" + page + "&amp;orderBy=tblItem.item_lot\"&gt;Lot&lt;/a&gt;&lt;/th&gt;\n&lt;th style=\"border-bottom:1px solid #000;\"&gt;&lt;a style=\"font-weight:bold; font-color:#000000;\" href=\"searchResults.aspx?page=" + page + "&amp;orderBy=tblItem.item_cs_txt_id\"&gt;Consignor ID&lt;/a&gt;&lt;/th&gt;\n&lt;th style=\"border-bottom:1px solid #000;\"&gt;&lt;a style=\"font-weight:bold; font-color:#000000;\" href=\"searchResults.aspx?page=" + page + "&amp;orderBy=tblMcat.mcat_name\"&gt;Master Category&lt;/a&gt;&lt;/th&gt;\n&lt;th style=\"border-bottom:1px solid #000;\"&gt;&lt;a style=\"font-weight:bold; font-color:#000000;\" href=\"searchResults.aspx?page=" + page + "&amp;orderBy=tblCat.cat_name\"&gt;Category&lt;/a&gt;&lt;/th&gt;\n&lt;th style=\"border-bottom:1px solid #000;\"&gt;&lt;a style=\"font-weight:bold; font-color:#000000;\" href=\"searchResults.aspx?page=" + page + "&amp;orderBy=tblItem.item_est_lo\"&gt;Low Est.&lt;/a&gt;&lt;/th&gt;\n&lt;th style=\"border-bottom:1px solid #000;\"&gt;&lt;a style=\"font-weight:bold; font-color:#000000;\" href=\"searchResults.aspx?page=" + page + "&amp;orderBy=tblItem.item_est_hi\"&gt;High Est.&lt;/a&gt;&lt;/th&gt;\n&lt;th style=\"border-bottom:1px solid #000;\"&gt;&lt;a style=\"font-weight:bold; font-color:#000000;\" href=\"searchResults.aspx?page=" + page + "&amp;orderBy=tblItem.item_reserve\"&gt;Reserve&lt;/a&gt;&lt;/th&gt;\n&lt;th style=\"border-bottom:1px solid #000;\"&gt;&lt;a style=\"font-weight:bold; font-color:#000000;\" href=\"searchResults.aspx?page=" + page + "&amp;orderBy=tblItem.item_status\"&gt;Status&lt;/a&gt;&lt;/th&gt;\n&lt;th style=\"border-bottom:1px solid #000;\"&gt;&lt;a style=\"font-weight:bold; font-color:#000000;\" href=\"searchResults.aspx?page=" + page + "&amp;orderBy=tblItem.item_pr\"&gt;Hammer Price&lt;/a&gt;&lt;/th&gt;\n&lt;th style=\"border-bottom:1px solid #000;\"&gt;&lt;a style=\"font-weight:bold; font-color:#000000;\" href=\"searchResults.aspx?page=" + page + "&amp;orderBy=tblMailList.mail_ID\"&gt;Bidder ID&lt;/a&gt;&lt;/th&gt;\n&lt;/tr&gt;\n";
      }
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here's the response.write code:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      Response.Write(Session["sort"] + " 1&lt;br/&gt;");
      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&lt;br/&gt;");
  }
  </code></pre>
  
  <p>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&lt;br/&gt;");</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).</p>
  
  <p>I figure I'll go ahead and add the logic to the whole order by/sort:</p>
  
  <pre><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></pre>