易学之家,八字,六爻,风水,奇门遁甲,六壬,金口诀,周易,阳宅风水,风水堪舆,张锋,风水罗盘指南针

Scroll Page Class 解决定位问题

       在开发B/S系统的时候,由于页面的刷新问题,经常会导致页面自动滚动到开始。然而有时候,我们需要页面在刷新后返回到页面前的位置。为了解决这个问题,我写了一个方法,大家可以参阅,其实也不是一种高明的做法,但至少解决了一些问题。

        原理就是在你需要的位置添加一个对象控件,让页面刷新后定位到此控件的位置,使用了scrollIntoView方法。

 

 public void ScrollToObject(string str_Ctl_Name,Page page)
{
    
if(!page.IsStartupScriptRegistered ("ScrollObject"
))
   
{
         page.RegisterStartupScript(
"msgScrollObject"," <script>document.forms(0)."+str_Ctl_Name+".scrollIntoView();document.forms(0)."+str_Ctl_Name+".focus(); </script>"
);
  }

}

 


注:ScrollToObject:方法名
        str_Ctl_Name:控件名
        例子:ScrollToObject(Button1,Page);

对于DataGrid的的定位问题,你可以使用HTML 中锚点的方式来解决,具体的操作可以参阅HTML手册来学习锚点是怎样运用的,这里就不再多谈了

 

以前的解决方法
原文地址:http://www.cnblogs.com/hbzxf/archive/2004/08/10/31916.aspx

无意中从网上查到的scrollPage Class 原文如下

Scroll Page

A while back, I saw a presentation on how to maintain your scroll position after a postback. I decided to create a quick page class you can derive from which will allow you to always maintain your scroll position. Enjoy! (Text version: http://scottwater.com/code/ScrollPage.cs.txt)

1:     using System;
2:     using System.IO;
3:     using System.Text;
4:     using System.Text.RegularExpressions;
5:     using System.Web.UI;
6:     
7:     namespace ScottWater.Pages
8:     {
9:         /// <summary>
10:        /// Summary description for ScrollPage.
11:        /// </summary>
12:        public class ScrollPage : System.Web.UI.Page
13:        {
14:            public ScrollPage()
15:            {
16:    
17:            }

18:    
19:            private bool _useScrollPersistence = true;
20:            /// <summary>
21:            /// There could be PostBack senarios where we do not want to remember the scroll position. Set this property to false
22:            /// if you would like the page to forget the current scroll position
23:            /// </summary>
24:            public bool UseScrollPersistence
25:            {
26:                get {return this._useScrollPersistence;}
27:                set {this._useScrollPersistence = value;}
28:            }

29:    
30:            private string _bodyID;
31:            /// <summary>
32:            /// Some pages might already have the ID attribute set for the body tag. Setting this property will not render the ID or change
33:            /// the existing value. It will simply update the javascript written out to the browser.
34:            /// </summary>
35:            public string BodyID
36:            {
37:                get {return this._bodyID;}
38:                set {this._bodyID = value;}
39:            }

40:    
41:    
42:            //Last chance. Do we want to maintain the current scroll position
43:            protected override void OnPreRender(EventArgs e)
44:            {
45:                if(UseScrollPersistence)
46:                {
47:                    RetainScrollPosition();
48:                }

49:                base.OnPreRender (e);
50:            }
        
51:    
52:            protected override void Render(HtmlTextWriter writer)
53:            {
54:                //No need processing the HTML if the user does not want to maintain scroll position or already has
55:                //set the body ID value
56:                if(UseScrollPersistence && BodyID == null)
57:                {
58:                    TextWriter tempWriter = new StringWriter();
59:                    base.Render(new HtmlTextWriter(tempWriter));
60:                    writer.Write(Regex.Replace(tempWriter.ToString(),"<body","<body id="thebody" ",RegexOptions.IgnoreCase));
61:                }

62:                else
63:                {
64:                    base.Render(writer);
65:                }

66:            }

67:    
68:            private static string saveScrollPosition = "<script language='javascript'>function saveScrollPosition() {{document.forms[0].__SCROLLPOS.value = {0}.scrollTop;}}{0}.onscroll=saveScrollPosition;</script>";
69:            private static string setScrollPosition = "<script language='javascript'>function setScrollPosition() {{{0}.scrollTop ="{1}";}}{0}.onload=setScrollPosition;</script>";
70:    
71:            //Write out javascript and hidden field
72:            private void RetainScrollPosition()
73:            {
74:                RegisterHiddenField("__SCROLLPOS""0");
75:                string __bodyID = BodyID == null ? "thebody" : BodyID;
76:                RegisterStartupScript("saveScroll"string.Format(saveScrollPosition,__bodyID));
77:    
78:                if(Page.IsPostBack)
79:                {
80:                    RegisterStartupScript("setScroll"string.Format(setScrollPosition,__bodyID, Request.Form["__SCROLLPOS"]));
81:                }

82:            }

83:        }

84:    }

posted on 2004-09-15 09:50  张锋  阅读(1474)  评论(0编辑  收藏  举报

易学之家,八字,六爻,风水,奇门遁甲,六壬,金口诀,周易,阳宅风水,风水堪舆,张锋,风水罗盘指南针