Click here to Skip to main content
15,886,137 members
Articles / Web Development / ASP.NET

Smart Navigation in Scrollable Web Controls

Rate me:
Please Sign up or sign in to vote.
3.65/5 (21 votes)
1 Dec 2005CPOL2 min read 87.9K   37   15
An article on maintaining smart navigation in web controls between page post backs.

Introduction

Nowadays it is very common among programmers to use the HTML <div> tag to display scrollable datagrids. But during post backs of ASP.NET pages, maintaining the scroll position is always a tedious task, and if a page contains multiple scrollable grids, then it causes some real problems to maintain the original scroll position for grids. In this article, I am going to present a very easy way to maintain scroll position of various <div> tags as well as how to maintain the smart navigation of an entire page with it.

The DataGrid control of Microsoft .NET does not have an in-built scrollbar, and to display a scrollable DataGrid, we need to embed the DataGrid control within an HTML <div>. It is very easy to display such a DataGrid like this:

HTML
<div id="div1" style="OVERFLOW: auto; WIDTH: 100%; 
         BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; 
         HEIGHT: 200px; BORDER-BOTTOM-STYLE: none">
    Data Grid can be placed here.
</div>

Setting Smart Navigation of Page Through JavaScript

This very simple JavaScript code provides smart navigation and maintains the scroll position of the entire web page. This process uses the window object and the cookies collection of the page. The code is:

JavaScript
<script language = "javascript">
    ///Attaching a function on window.onload event.
    window.onload = function()
    {
        var strCook = document.cookie; 
        if(strCook.indexOf("!~")!=0)
        { 
            var intS = strCook.indexOf("!~"); 
            var intE = strCook.indexOf("~!"); 
            var strPos = strCook.substring(intS+2,intE); 
            document.body.scrollTop = strPos; 
        } 
    }
    /// Function to set Scroll position of page before postback. 
    function SetScrollPosition()
    { 
        var intY = document.body.scrollTop; 
        document.cookie = "yPos=!~" + intY + "~!"; 
    }
    /// Attaching   SetScrollPosition() function to window.onscroll event.
    window.onscroll = SetScrollPosition;
</script>

The above JavaScript code sets the vertical scroll position of the page in the page cookie section, and on window.onload, it retrieves back the scrolling position from the cookie section and sets document.body.scrollTop according to its value.

Setting the Scroll Position of a <div> Containing Controls Through JavaScript

Now suppose you have a scrollable DataGrid on a page; it is very simple to associate the scroll positions of the <div> containing the DataGrid to the page cookie section, and then on the same window.onload event, we can set the scroll position of the <div> as well. To do this, we need to add a new function which executes the OnScroll event of the <div> and set the scroll position to the page cookie and a modification in the function attached with the window.onload event:

JavaScript
<script language = "javascript">
    
///This function sets the scroll position of div to cookie.
function setScrollPos()
{
    var divY = document.getElementById('div1').scrollTop;
    document.cookie = "divPos=!*" + divY + "*!";
}

///Attaching a function on window.onload event.
window.onload = function()
{
    var strCook = document.cookie;
    if(strCook.indexOf("!~")!=0)
    {
        var intS = strCook.indexOf("!~");
        var intE = strCook.indexOf("~!");
        var strPos = strCook.substring(intS+2,intE);
        document.body.scrollTop = strPos;
    }
    /// This condition will set scroll position od <div>.
    if(strCook.indexOf("!*")!=0)
    {
        var intdS = strCook.indexOf("!*");
        var intdE = strCook.indexOf("*!");
        var strdPos = strCook.substring(intdS+2,intdE);
        document.getElementById('div1').scrollTop = strdPos;
    }
}
/// Function to set Scroll position of page before postback.
function SetScrollPosition()
{
    var intY = document.body.scrollTop;
    document.cookie = "yPos=!~" + intY + "~!";
}
/// Attaching   SetScrollPosition() function to window.onscroll event.
window.onscroll = SetScrollPosition;
</script>

Now we need to call the setScrollPos() function on the <div> tag's onScroll event. To do this, modify the HTML specification of the <div> like this:

JavaScript
<div id="div1" style="OVERFLOW: auto; WIDTH: 100%; BORDER-TOP-STYLE: none; 
   BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; HEIGHT: 200px; 
   BORDER-BOTTOM-STYLE: none" onscroll="setScrollPos();">
          Data Grid can be placed here.
</div>

The above simple procedure provides smart navigation to scrolling <div> tags irrespective of what the <div> contains, but a very common use of it is in scrollable datagrids. Hope this article has given you a better idea to provide smart navigation on web pages.

Thanks in advance for your comments.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Currently working as a Senior Developer and enjoying software development from more than 8 years on .NET.

Comments and Discussions

 
QuestionThank you so much! Pin
Member 1145055018-Mar-15 8:22
Member 1145055018-Mar-15 8:22 
GeneralMy vote of 5 Pin
SimonIgate3-Aug-10 5:40
SimonIgate3-Aug-10 5:40 
Generalfunctionality not working Pin
k.goutam00818-Jun-08 1:51
k.goutam00818-Jun-08 1:51 
Generalit's good and helpful Pin
hari1029-May-07 23:36
hari1029-May-07 23:36 
thanks mukesh
Generaldinamic script to all div that you have Pin
AqsakMaboul3-May-07 23:34
AqsakMaboul3-May-07 23:34 
GeneralVery helpful.................... Pin
tprashanth10-Apr-07 1:15
tprashanth10-Apr-07 1:15 
GeneralNice!!! Pin
Veera V Satya N Kanithi10-Jan-07 11:10
Veera V Satya N Kanithi10-Jan-07 11:10 
GeneralRe: Nice!!! Pin
JoeReynolds26-Feb-07 17:33
JoeReynolds26-Feb-07 17:33 
GeneralASP.NET 2.0 always complains onscroll of a div Pin
kekelele14-Nov-06 8:13
kekelele14-Nov-06 8:13 
GeneralUse of Smart Navigation in Div interface Pin
Dominic Turner30-Oct-06 23:18
Dominic Turner30-Oct-06 23:18 
GeneralThanks this helps me to solve the problem!! Pin
smileblue28-Jun-06 7:59
smileblue28-Jun-06 7:59 
GeneralRe: Thanks this helps me to solve the problem!! Pin
robocato13-Jul-06 23:26
robocato13-Jul-06 23:26 
GeneralThanks this was useful to me Pin
froman11828-Dec-05 14:08
froman11828-Dec-05 14:08 
GeneralRead this before rating the article Pin
Mukesh Kumar Gupta2-Dec-05 2:13
Mukesh Kumar Gupta2-Dec-05 2:13 
QuestionVery usefull, but could be upgraded Pin
Wouter DW1-Mar-06 1:01
Wouter DW1-Mar-06 1:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.