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.