Amaranth
Amaranth

Reputation: 2491

Keeping the scrollbar position of a label

I have a little problem with my dynamically created controls. I have a asp.net page using c# code behind. I generated rows for a table (inside an update panel). In one of those rows, I have a label created with content and a vertical scrollbar. I also have a timer calling a postback every 5 seconds.

Label diff = new Label();
diff.Style.Add("height", height + "px");
diff.Style.Add("width", "100%");
diff.BorderWidth = 2;
diff.Style.Add("overflow-y", "scroll");
diff.Text = text;
cell.Controls.Add(diff);

The problem is that when the postback occurs, the row is deleted and created again, so the scroll position is lost.

I would like to keep the scroll position in a variable to be able to set it to the same position after the postback. Is there a way to do this?

Upvotes: 1

Views: 759

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460138

You could use the jQuery plugin ScrollSaver.

<script type="text/javascript" src="scrollsaver.min.js"></script>

All you have to do is include the script on the page and before a Postback occurs it will save the location of each element in a cookie and then restore it when the page re-renders.

Upvotes: 3

Related Questions