简体   繁体   中英

ASP.NET Setting Session Variable to control display of deleted items works 2nd time?

I have a grid that is populated on Page_Load. The first time showing user data from the database excluding deleted items (I have a field in the database called RecordIsDeletedYN which if set true means the record is deleted). So my Page_Load code looks like this:

if(Session["ShowDeletedItems"] != null)
    ShowDeletedItems = Convert.ToBoolean(Session["ShowDeletedItems"]);
else
    ShowDeletedItems = false;

PopulateGrid();

I have a page variable declared at the top of the form:

private bool ShowDeletedItems = false;

My thinking is that the first time the page is loaded the above code sets the page variable to false.

Then the Page_Load calls my PopulateGrid function. In there I have an SQL query that is constructed based on the value of the ShowDeletedItems bool variable.

If ShowDeletedItems is false then a WHERE statement is included in the SQL Query that adds:

strSQL += "WHERE (RecordIsDeletedYN = 0) ";

So on start up ShowDeletedItems is false so the SQL WHERE is included and we don't show Deleted Items in the grid! This works!

Also on my screen is a button that when pressed looks at the state of the page Variable if it's false then it flips it to true and if its false it flips it to false. This is the code:

protected void imgButtShowDeleted_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
    if(ShowDeletedItems)
        Session["ShowDeletedItems"] = "False";
    else
        Session["ShowDeletedItems"] = "True";

}

This setup works but ONLY after the button is pressed a second time and thereafter it works perfectly on each subsequent press.

Where am I going wrong. I know it must be something to do with the way page events fire. I tried initialising the Session variable in Page_Init to false but when I did that it never shows the deleted items. The session variable is always set false.

Any help appreciated with this.

You set ShowDeletedItems in the Page.Load event handler. According to ASP.NET Page Life Cycle , the Load stage occurs before the Event handling stage. So, the Page.Load event is raised before any Control-changed event .

In your case, the Page_Load handler is called before the imgButtShowDeleted_Click handler. Therefore you see the updated data only after additional postback after clicking the button.

To solve this issue, re-bind the grid after you changed the ShowDeletedItems variable in the imgButtShowDeleted_Click event handler.

Avoid the page level var for confusion...have a get/set property on the page that directly gets or sets your session var...it'll simplify your code and make it less likely to have such problems.

Also consider if it's page only and doesn't require rentention across new page requests...consider using viewstate instead of session.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM