how to detect if a page is refreshed?

It depends on what your “Balance” field is (that is some information you did not share).

It is also unclear where your “balance” is originating from.

It is definitely a bad idea to write the “Balance” to the database, it should rather be calculated.

Next question is what should happen if the user changes the sorting of the page, should it again start with the first record shown ?

What you would need is a function which calculates the Balance by looping through the records:

Something like:

OnAfterGetRecord()
Rec2.COPY(Rec);
Balance := Amount;
WHILE Rec2.NEXT(-1) = -1 DO
Balance += Rec2.Amount;

Whereby Balance should be a Global of the type Decimal, rather than a field in the table.