.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

How to go back to the same position in a given view inside a multiview

Posted By:Kemal AL GAZZAH       Posted Date: February 24, 2011    Points: 10    Category: ASP.NET    URL: http://www.dotnetspark.com  

This article deals about how to go back to the same position in a given view inside a multiview for all asp.net pages in a web project using master pages.
 

How to go back to the same position in a given view inside a multiview for all asp.net pages in a web project using master pages ?

Multiviews are commonly used in asp.net pages, but one flaw is observed by users which is the following:

Suppose you have web page using Multiview and which is too long that you have to use the vertical scroll bar to focus on a control in the bottom, and that you click on a link to select some data for example , which are dispalyed in a different view of the multiview.
If you go to this view, then you go back to the original view, you will be positioned in the top of the page and you need to click on vertical scroll bar to go back to the original position, this is not convenient for users especially when they are using this page frequently.

I found the following article

http://www.4guysfromrolla.com/articles/111704-1.aspx

which allows to maintain scroll bar position in postbacks, but does not manage mutliple multiviews: My Web application is using many multiviews in the same webform, so I used the code of that article and did changes to allow to manage many multiviews in web forms using master page as follows:

1) Create the following Javascript functions:

function sstchur_SmartScroller_GetCoords() {
    var scrollX, scrollY, idx, idy;

    if (document.all) {
        if (!document.documentElement.scrollLeft)
            scrollX = document.body.scrollLeft;
        else
            scrollX = document.documentElement.scrollLeft;

        if (!document.documentElement.scrollTop)
            scrollY = document.body.scrollTop;
        else
            scrollY = document.documentElement.scrollTop;
    }
    else {
        scrollX = window.pageXOffset;
        scrollY = window.pageYOffset;
    }

 

    if (document.getElementById("ctl00_mvid") != null) {
        idx = "ctl00_hx_" + document.getElementById("ctl00_mvid").value;
        idy = "ctl00_hy_" + document.getElementById("ctl00_mvid").value;

        if (document.getElementById(idx) != null) {
            document.getElementById(idx).value = scrollX;
        }
        if (document.getElementById(idy) != null) {
            document.getElementById(idy).value = scrollY;
        }
    }
}

function sstchur_SmartScroller_Scroll() {

    var idx, idy;

    if (document.getElementById("ctl00_mvid") != null) {
        idx = "ctl00_hx_" + document.getElementById("ctl00_mvid").value;
        idy = "ctl00_hy_" + document.getElementById("ctl00_mvid").value;

        if (document.getElementById(idx) != null) {var x = document.getElementById(idx).value;}
        if (document.getElementById(idy) != null) { var y = document.getElementById(idy).value; }
        if (x != null | y != null) { window.scrollTo(x, y); }
    }
}


2) Add the following function to masterpage.vb

Sub create_hiddenfields_xy()

        Dim i As Integer
        Dim hf As HiddenField
        If CType(Me.ContentPlaceHolder1.FindControl("Multiview1"), MultiView) IsNot Nothing Then
            For i = 0 To CType(Me.ContentPlaceHolder1.FindControl("Multiview1"), MultiView).Views.Count - 1
                hf = New HiddenField
                hf.ID = "hx_" & i
                Me.form1.Controls.Add(hf)
                hf.Value = "0"

                hf = New HiddenField
                hf.ID = "hy_" & i
                Me.form1.Controls.Add(hf)
                hf.Value = "0"

            Next

        End If
End Sub

3) To call this function in page_load
 and that's it !
Now you can go back to the original position in any web form of your whole application !

 

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend