MaintainScrollPositionOnPostback

이번 Graduate Phase II 프로젝트의 요건중의 하나로, Postback이 일어날 때, scroll position을 유지해줘야 하는게 있다. 아직 .Net 1.1을 쓰니까, .Net 2에 있는 client script를 잘 쓰면 될 것도 같은데.

Reference 나가신다.

4guys from roll에 있는 소스 코드를 조금 수정해서 만들어 보았다. 잘 된다. 4 guys 고마워요!

  141         private void SetWindowScrollPosition()

  142         {

  143             if (!IsPostBack)

  144             {

  145                 //For when softloggedin cookie is removed and page is refreshed.

  146                 hidScrollLeft.Value = PageUtility.ParseQueryStringInt(“x”).ToString();

  147                 hidScrollTop.Value = PageUtility.ParseQueryStringInt(“y”).ToString();

  148             }

  149 

  150             Page.RegisterStartupScript(“GetWindowScrollPosition”, string.Format(@”

  151 <script type=’text/javascript’>

  152 <!–

  153   function GetScrollPosition()

  154   {{

  155     var scrollX, scrollY;

  156     if (document.all) {{

  157       if (!document.documentElement.scrollLeft)

  158         scrollX = document.body.scrollLeft;

  159       else

  160         scrollX = document.documentElement.scrollLeft;

  161 

  162       if (!document.documentElement.scrollTop)

  163         scrollY = document.body.scrollTop;

  164       else

  165         scrollY = document.documentElement.scrollTop;

  166     }}

  167     else {{

  168       scrollX = window.pageXOffset;

  169       scrollY = window.pageYOffset;

  170     }}

  171 

  172     document.getElementById(‘{0}’).value = scrollX;

  173     document.getElementById(‘{1}’).value = scrollY;

  174   }}

  175 

  176   window.onscroll = GetScrollPosition;

  177   window.onclick = GetScrollPosition;

  178   window.onkeypress = GetScrollPosition;

  179 // –>

  180 </script>

  181 “,hidScrollLeft.ClientID, hidScrollTop.ClientID));

  182 

  183             Page.RegisterStartupScript(“SetWindowScrollPosition”,

  184                 string.Format(“<script type=’text/javascript’>window.scrollTo({0}, {1});</script>”, hidScrollLeft.Value, hidScrollTop.Value));

  185         }

MaintainScrollPositionOnPostback

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s