0

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.

Siv
  • 125
  • 2
  • 14

2 Answers2

1

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.

Vladimir
  • 828
  • 6
  • 8
  • Vladimir, Adding a call to the populate routine in the imgButtShowDeleted_Click routine worked!!! However I have another button that allows you to edit the selected item and when I click this it always reloads the page and then I get an error saying there was nothing selected in the grid. So for some reason the grid's state seems to change so that what was selected in the grid is not selected when the button I pressed reloads the page? This page lifecycle stuff is very confusing! – Siv Jan 10 '18 at 16:07
  • I need to see the code of the problematic page to find the cause. Could you demonstrate how you manage the grid on the server side? – Vladimir Jan 11 '18 at 07:16
  • Vladimir, I am using a DevExpress ASPxGridView. I populate it using a routine called PopulateGrid. I call this at Page_Init. This is the code: – Siv Jan 12 '18 at 10:16
  • I tried to add the code but it exceeds the allowed character limit in a comment!? Is there a way I can post it here as I can't figure out how you do it other than using these comments? – Siv Jan 12 '18 at 10:22
  • Can you update your initial post or upload your files to a shared source and send a link? – Vladimir Jan 15 '18 at 13:43
  • 1
    Vladimir, there is no option to edit my original post, however I have now fixed the issue. I changed the Page_Init to Page_Load and now the selection is seen as being selected and I changed the imagebutton to a check box so that the state of the checkbox persists and now editing works as expected. Thanks for your help, the switch to populating the screen in Page_Load was the key answer. – Siv Jan 17 '18 at 16:19
0

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.

Ctznkane525
  • 7,297
  • 3
  • 16
  • 40
  • I see your point, can you explain why ViewState is preferable? – Siv Jan 09 '18 at 00:06
  • I added the Get Set and used ViewState and it still behaves the same? I am also getting another issue in that I have an edit button which takes the selected row in the grid and takes you to another page that allows you to edit the selected user's record and that when clicked makes the form reload and loses the selection? – Siv Jan 09 '18 at 00:45