Wednesday, 2 January 2013

To maintain Div scroll position after postback on User Control :

To maintain Div scroll position after postback on User Control :


   Ascx Page:     

                   <script type="text/javascript">
                    window.onload = function () {
                   var h = document.getElementById("<%=hfScrollPosition.ClientID%>");
                   document.getElementById("<%=scrollArea.ClientID%>").scrollTop = h.value;
                  }
          function SetDivPosition() {
               var intY = document.getElementById("<%=scrollArea.ClientID%>").scrollTop;
               var h = document.getElementById("<%=hfScrollPosition.ClientID%>");
               h.value = intY;
         }

           function afterpostback() {

                var h = document.getElementById("<%=hfScrollPosition.ClientID%>");
                document.getElementById("<%=scrollArea.ClientID%>").scrollTop = h.value;
        
         }

    </script>

     <asp:HiddenField ID="hfScrollPosition" runat="server" Value="0" />
     <div id="scrollArea" onscroll="SetDivPosition()"   runat="server"  style="height:225px;overflow:auto;overflow-x:hidden;">


DescriptionWhenever the div is scrolled,the value get stored to the hidden field, To retain the value after postback or retain the scroll postion after postback, please use the below line in ur page_load event,
VB.net :


         If Page.IsPostBack Then
             ScriptManager.RegisterClientScriptBlock(Page, Me.GetType(), "CallJS", "afterpostback();", True)
         End If


C# coding :


        if (Page.IsPostBack) {
        ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJS", "afterpostback();", true);
        }







1 comment: