Click here to Skip to main content
6,918,336 members and growing! (22,812 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Beginner

Smart Navigation In Scrollable Web Controls.

By Mukesh Kumar Gupta

An article on maintaining smart navigation in web controls between page post backs.
C#, Javascript, Windows, .NET, ASP.NET, Visual-Studio, Dev
Posted:23 Nov 2005
Updated:2 Dec 2005
Views:46,605
Bookmarked:32 times
Unedited contribution
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
19 votes for this article.
Popularity: 4.56 Rating: 3.56 out of 5
2 votes, 10.5%
1
1 vote, 5.3%
2
3 votes, 15.8%
3
2 votes, 10.5%
4
11 votes, 57.9%
5
 

Introduction

Now a days it is very common between programmers to use HTML <div> tag to display scrollable Data Grids. 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 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 entire page with it.

Data Grid control of Microsoft .NET does not have a inbuilt scroll bar and to display scrollable Data Grid we need to embed Data Grid control within a HTML <div>. It is very easy to display such Data Grid like this: 

<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 Java Script

A very simple code snippet of Java Script provides smart navigation and maintains scroll position of entire web page, this process uses window object and cookies collection of page. The code is:

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

Above Java Script sets the vertical scroll position of page in page cookie section and on window.onload it retrieve back scrolling position from cookie section and set document.body.scrollTop according to it.  

Setting Scroll Position of <div> Containing Controls Through Java Script

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

<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 setScrollPos() function on <div> tags onScroll event to do this modify HTML specification of <div> like this:

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

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 Data Grids. 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Mukesh Kumar Gupta


Member
Currently working as a Developer Analyst and enjoying software development from more than 4 years on VB, ASP, .NET, Java, J2EE and various development tools.
Occupation: Web Developer
Location: India India

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 13 of 13 (Total in Forum: 13) (Refresh)FirstPrevNext
Generalfunctionality not working Pinmemberk.goutam0082:51 18 Jun '08  
Generalit's good and helpful Pinmemberhari1020:36 10 May '07  
Generaldinamic script to all div that you have PinmemberAqsakMaboul0:34 4 May '07  
GeneralVery helpful.................... Pinmembertprashanth2:15 10 Apr '07  
GeneralNice!!! PinmemberSatya Kanithi12:10 10 Jan '07  
GeneralRe: Nice!!! PinmemberJoeReynolds18:33 26 Feb '07  
GeneralASP.NET 2.0 always complains onscroll of a div Pinmemberkekelele9:13 14 Nov '06  
GeneralUse of Smart Navigation in Div interface PinmemberDominic Turner0:18 31 Oct '06  
GeneralThanks this helps me to solve the problem!! Pinmembersmileblue8:59 28 Jun '06  
GeneralRe: Thanks this helps me to solve the problem!! Pinmemberrobocato0:26 14 Jul '06  
GeneralThanks this was useful to me Pinmemberfroman11815:08 28 Dec '05  
GeneralRead this before rating the article PinmemberMukesh Kumar Gupta3:13 2 Dec '05  
QuestionVery usefull, but could be upgraded PinmemberWouter DW2:01 1 Mar '06  
Isn't it possible to make the code more dynamic? Why use !~ and !*Confused ?

If you write out the strCook-variable you get "yPos=!~135~!; divPos=!*59*!" (without the quotes)

If you split this on ";", then you have name-value-couples you have to split on the first "=". So don't use !~ and !*.

If you also add the id's of the elements to the cookie, you can write code where you never have to change the onload-function (just get element-id and position from the cookie and adjust the value).
eg: id1=div1; div1pos=36; id2=newDiv; newDivpos=73; (that's just a thought, there could be better options than this)

And if you than change function setScrollPos() to one having the element-id as a parameter, this code also doesn't have to be changed at all.

In that case you can have hundreds of divs with scrollbars without having to write an other letter of javascript.

If I have the time I'll have a go at it, but time is very limmited for me and I am not used to writing javascript. Maybe there's somebody who knows more about writing javascript want's to post a more flexible version????

Greetings and thanks for the code.
Wouter - Belgium

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 2 Dec 2005
Editor:
Copyright 2005 by Mukesh Kumar Gupta
Everything else Copyright © CodeProject, 1999-2010
Web10 | Advertise on the Code Project