The code below only gives the expected result while viewing the page but not in edit mode. Any idea why?
int.TryParse(CurrentPage.Property["PageLink"], out i);
That's because PageLink is not always an int. In edit mode it also has a work id, so you need to treat PageLink as a string. If you just want the just the id, you can do CurrentPage.PageLink.ID.
var pageReference = CurrentPage.Property["PageLink"] as PageReference;
var id = pageReference.ID;