Program,Life,Society.....

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

From scottwater.com->Scroll Page


C#

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:    }

 

 

 

VB.NET

 

Imports System

Imports System.IO

Imports System.Text

Imports System.Text.RegularExpressions

Imports System.Web

Imports System.Web.UI

 

Namespace ScottWater.Pages

 

    Public Class ScrollPage

        Inherits System.Web.UI.Page

 

        Public Sub ScrollPage()

 

        End Sub

 

        Private m_UseScrollPersistence As Boolean = True

        Private m_BodyID As String

        Private saveScrollPosition As String = "<script language='javascript'>function saveScrollPosition() {{document.forms[0].__SCROLLPOS.value = {0}.scrollTop;}}{0}.onscroll=saveScrollPosition;</script>"

        Private setScrollPosition As String = "<script language='javascript'>function setScrollPosition() {{{0}.scrollTop =""{1}"";}}{0}.onload=setScrollPosition;</script>"

 

        Public Property UseScrollPersistence() As Boolean

            Get

                Return m_UseScrollPersistence

            End Get

            Set(ByVal Value As Boolean)

                m_UseScrollPersistence = Value

            End Set

        End Property

 

        Public Property BodyID() As String

            Get

                Return m_BodyID

            End Get

            Set(ByVal Value As String)

                m_BodyID = Value

            End Set

        End Property

 

        Protected Overrides Sub OnPreRender(ByVal e As EventArgs)

            If UseScrollPersistence Then

                'call the routine to add the javascript

                RetainScrollPosition()

            End If

 

            MyBase.OnPreRender(e)

        End Sub

 

        Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)

            If UseScrollPersistence And (BodyID = Nothing) Then

                Dim tempWriter As TextWriter = New StringWriter

                MyBase.Render(New HtmlTextWriter(tempWriter))

                writer.Write(Regex.Replace(tempWriter.ToString(), "<body", "<body id=""thebody"" ", RegexOptions.IgnoreCase))

            Else

                MyBase.Render(writer)

            End If

        End Sub

 

        Private Sub RetainScrollPosition()

            Me.Page.RegisterHiddenField("__SCROLLPOS", "0")

            Dim s_bodyID As String

            If BodyID = Nothing Then s_bodyID = "thebody" Else s_bodyID = BodyID

            RegisterStartupScript("saveScroll", String.Format(saveScrollPosition, s_bodyID))

            If (Page.IsPostBack) Then

                RegisterStartupScript("setScroll", String.Format(setScrollPosition, s_bodyID, context.Request.Form("__SCROLLPOS")))

            End If

        End Sub

 

 

    End Class

 

End Namespace

 

posted on 2005-04-19 16:58  vuejs3  阅读(898)  评论(1编辑  收藏  举报