1/// <summary>
2/// 保存div与页面滚动条的位置
3 /// </summary>
9/// <param name="divId">DIV的ID数组</param>
10public static void RetainScrollPosition(string[] divId)
11{
12
13 System.Web.UI.Page page=(Page)HttpContext.Current.Handler;
14
15string saveScrollPosition = "";
16string setScrollPosition = "";
17
18string saveScrollPositionDiv = "";
19string setScrollPositionDiv = "";
20
21#region 保存页面滚动条的状态
22 page.RegisterHiddenField("__SCROLLPOS_TOP","0");
23 page.RegisterHiddenField("__SCROLLPOS_LEFT","0");
24
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
32#region 保存divId的位置
33for (int i = 0; i < divId.Length; i++)
34{
35 page.RegisterHiddenField(divId[i].ToString() + "_TOP","0");
36 page.RegisterHiddenField(divId[i].ToString() + "_LEFT","0");
37
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
41 saveScrollPositionDiv = string.Format(saveScrollPositionDiv,"window.document.all('" + divId[i].ToString() + "')");
42 page.RegisterStartupScript("saveScroll" + divId[i].ToString(), saveScrollPositionDiv);
43
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
48#region 注册客户端脚本
49 saveScrollPosition = "<script language='javascript'>function saveScrollPosition() {{" + saveScrollPosition + "}}{0}.onscroll=saveScrollPosition;</script>";
50 page.RegisterStartupScript("saveScroll", string.Format(saveScrollPosition,"window.document.body"));
51
52if(page.IsPostBack)
53{
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
60 }
2/// 保存div与页面滚动条的位置
3 /// </summary>
9/// <param name="divId">DIV的ID数组</param>
10public static void RetainScrollPosition(string[] divId)
11{
12
13 System.Web.UI.Page page=(Page)HttpContext.Current.Handler;
14
15string saveScrollPosition = "";
16string setScrollPosition = "";
17
18string saveScrollPositionDiv = "";
19string setScrollPositionDiv = "";
20
21#region 保存页面滚动条的状态
22 page.RegisterHiddenField("__SCROLLPOS_TOP","0");
23 page.RegisterHiddenField("__SCROLLPOS_LEFT","0");
24
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
32#region 保存divId的位置
33for (int i = 0; i < divId.Length; i++)
34{
35 page.RegisterHiddenField(divId[i].ToString() + "_TOP","0");
36 page.RegisterHiddenField(divId[i].ToString() + "_LEFT","0");
37
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
41 saveScrollPositionDiv = string.Format(saveScrollPositionDiv,"window.document.all('" + divId[i].ToString() + "')");
42 page.RegisterStartupScript("saveScroll" + divId[i].ToString(), saveScrollPositionDiv);
43
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
48#region 注册客户端脚本
49 saveScrollPosition = "<script language='javascript'>function saveScrollPosition() {{" + saveScrollPosition + "}}{0}.onscroll=saveScrollPosition;</script>";
50 page.RegisterStartupScript("saveScroll", string.Format(saveScrollPosition,"window.document.body"));
51
52if(page.IsPostBack)
53{
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
60 }
本文转自高海东博客园博客,原文链接:http://www.cnblogs.com/ghd258/archive/2005/11/07/270441.html,如需转载请自行联系原作者