.NET技术支持者

  博客园 :: 首页 :: 博问 :: 闪存 :: :: 联系 :: 订阅 订阅 :: 管理 ::
1/// <summary>
 2        /// 保存div与页面滚动条的位置
 3        /// </summary>
 9        /// <param name="divId">DIV的ID数组</param>

10        public static void RetainScrollPosition(string[] divId)
11        {
12            
13            System.Web.UI.Page page=(Page)HttpContext.Current.Handler;
14
15            string saveScrollPosition = "";
16            string setScrollPosition = "";
17
18            string saveScrollPositionDiv = "";
19            string 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的位置
33            for (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
52            if(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        }
posted on 2005-11-07 09:29  LDAR泄漏检测与修复  阅读(1503)  评论(0编辑  收藏  举报