Session variable value changes between page processing somehow

放肆的年华 提交于 2019-12-11 08:17:45

问题


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 response.write(Session["sort"]) 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, Session["orderby"]).

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 response.write(Session["sort"]) 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 response.write(Session["sorty"]) 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:

    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=\"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=\"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=\"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=\"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=\"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=\"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=\"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=\"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=\"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=\"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=\"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=\"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=\"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=\"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=\"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=\"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=\"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=\"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=\"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=\"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=\"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=\"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=\"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=\"searchResults.aspx?page=" + page + "&orderBy=tblMailList.mail_ID\">Bidder ID</a></th>\n</tr>\n";
    }

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.

EDIT

Here's the response.write 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/>");
}

So because page is always set in the column headers, the get_page() method is called when I sort a column. orderBy is set through the query string and passed along to the method protected void get_page(Int32 page, String orderBy) which is what contains the below code. At the end of that method is also a Response.Write(Session["sort"] + " 2<br/>"); 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:

    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";
    }

回答1:


I'm going to guess you're experiencing this in IE.

Further, I'm going to guess that it's likely that there is an image source that is invalid and so it's posting back to the root. We used to have this problem as well, and you never caught it until you watched the network traffic with something like FireBug or Fiddler. Our application used to literally just logout because of the code that existed in the Page_Load.

Effectively what's going to happen is when an image URL fails, ASP.NET is going to receive a request at the root of the page and thus post back and wipe out the information.

Also, follow @peer advice.




回答2:


When I read you post I'm wondering why you put the variables in the session, the variables are in the context of the control/page so you should put them in the ViewState. When you put them in the Session, it will be shared over multiple pages in your site, which can result in the behavour you have. I guess that when you use the back button on your site, you will will also have unexpected behavour, which is caused by storing variables in the session instead of the viewstate.

Replace all Session to ViewState (orderBy and sort) and see if you still have the strange behavour.



来源:https://stackoverflow.com/questions/12623691/session-variable-value-changes-between-page-processing-somehow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!