1
/// <summary>
2
/// 保存div与页面滚动条的位置
3
/// </summary>
9
/// <param name="divId">DIV的ID数组</param>
10
public
static
void RetainScrollPosition(
string[] divId)
11
{
12![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
13
System.Web.UI.Page page=(Page)HttpContext.Current.Handler;
14![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
15
string saveScrollPosition = "";
16
string setScrollPosition = "";
17![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
18
string saveScrollPositionDiv = "";
19
string setScrollPositionDiv = "";
20![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
21![ContractedSubBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
#region 保存页面滚动条的状态
22
page.RegisterHiddenField("__SCROLLPOS_TOP","0");
23
page.RegisterHiddenField("__SCROLLPOS_LEFT","0");
24![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
25
saveScrollPosition = "document.forms[0].__SCROLLPOS_TOP.value = {0}.scrollTop;document.forms[0].__SCROLLPOS_LEFT.value = {0}.scrollLeft;";//}{0}.onscroll=saveScrollPosition;</script>";
26
setScrollPosition = "{0}.scrollTop ='{1}';{0}.scrollLeft ='{2}';";//}{0}.onload=setScrollPosition;</script>";
27
28
saveScrollPosition = string.Format(saveScrollPosition,"window.document.body");
29
setScrollPosition = string.Format(setScrollPosition,"window.document.body", page.Request.Form["__SCROLLPOS_TOP"],page.Request.Form["__SCROLLPOS_LEFT"]);
30
#endregion
31![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
32![ContractedSubBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
#region 保存divId的位置
33
for (int i = 0; i < divId.Length; i++)
34![ExpandedSubBlockStart.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
{
35
page.RegisterHiddenField(divId[i].ToString() + "_TOP","0");
36
page.RegisterHiddenField(divId[i].ToString() + "_LEFT","0");
37![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
38
saveScrollPositionDiv = "<script language='javascript'>function saveScrollPosition" + divId[i].ToString() + "() {{document.all('" + divId[i].ToString() + "_TOP').value = {0}.scrollTop;document.all('" + divId[i].ToString() + "_LEFT').value = {0}.scrollLeft;}}{0}.onscroll=saveScrollPosition" + divId[i].ToString() + ";</script>";
39
setScrollPositionDiv += "{0}.scrollTop = {1};{0}.scrollLeft = {2};";
40![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
41
saveScrollPositionDiv = string.Format(saveScrollPositionDiv,"window.document.all('" + divId[i].ToString() + "')");
42
page.RegisterStartupScript("saveScroll" + divId[i].ToString(), saveScrollPositionDiv);
43![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
44
setScrollPositionDiv = string.Format(setScrollPositionDiv,"window.document.all('" + divId[i].ToString() + "')", page.Request.Form[divId[i].ToString() + "_TOP"],page.Request.Form[divId[i].ToString() + "_LEFT"]);
45
}
46
#endregion
47![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
48![ContractedSubBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
#region 注册客户端脚本
49
saveScrollPosition = "<script language='javascript'>function saveScrollPosition() {{" + saveScrollPosition + "}}{0}.onscroll=saveScrollPosition;</script>";
50
page.RegisterStartupScript("saveScroll", string.Format(saveScrollPosition,"window.document.body"));
51![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
52
if(page.IsPostBack)
53![ExpandedSubBlockStart.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
{
54
setScrollPosition = "<script language='javascript'>function setScrollPosition() {{" + setScrollPosition + setScrollPositionDiv + "}}{0}.onload=setScrollPosition;</script>";
55
page.RegisterStartupScript("setScroll", string.Format(setScrollPosition,"window.document.body"));
56
//page.RegisterStartupScript("setScrollDocument", string.Format(setScrollPosition,"window.document.body", page.Request.Form["__SCROLLPOS_TOP"],page.Request.Form["__SCROLLPOS_LEFT"]));
57
}
58
#endregion
59![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
60
}
![ExpandedBlockStart.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![ContractedBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
2
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
3
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
9
![ExpandedBlockEnd.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
10
![None.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
11
![ExpandedBlockStart.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![ContractedBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
12
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
13
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
14
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
15
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
16
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
17
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
18
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
19
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
20
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
21
![ContractedSubBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
22
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
23
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
24
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
25
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
26
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
27
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
28
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
29
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
30
![ExpandedSubBlockEnd.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
31
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
32
![ContractedSubBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
33
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
34
![ExpandedSubBlockStart.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![ContractedSubBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
35
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
36
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
37
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
38
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
39
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
40
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
41
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
42
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
43
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
44
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
45
![ExpandedSubBlockEnd.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
46
![ExpandedSubBlockEnd.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
47
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
48
![ContractedSubBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
49
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
50
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
51
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
52
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
53
![ExpandedSubBlockStart.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![ContractedSubBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
54
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
55
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
56
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
57
![ExpandedSubBlockEnd.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
58
![ExpandedSubBlockEnd.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
59
![InBlock.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
60
![ExpandedBlockEnd.gif](https://zsy.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
本文转自高海东博客园博客,原文链接:http://www.cnblogs.com/ghd258/archive/2005/11/07/270441.html,如需转载请自行联系原作者