2011-09-12 74 views
6
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server"> 
    <meta http-equiv="refresh" content="4" /> 
<script type="text/javascript"> 

    var xPos1, yPos1; 

    var prm = Sys.WebForms.PageRequestManager.getInstance(); 
    prm.add_pageLoading(pageLoadingHandler); 
    prm.add_pageLoaded(pageLoaded); 
    function pageLoaded(sender, args) { 

     $get('<%=Panel_Users.ClientID %>').scrollLeft = xPos1; 
     $get('<%=Panel_Users.ClientID %>').scrollTop = yPos1; 
    } 
    function pageLoadingHandler(sender, args) { 
     xPos1 = $get('<%=Panel_Users.ClientID %>').scrollLeft 
     yPos1 = $get('<%=Panel_Users.ClientID %>').scrollTop; 
    } 
    </script> 
</asp:Content> 

不工作後,頁面滾動位置,我要去哪裏錯了如何保持在asp.net頁面回發

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />  

<div style="height: 504px; width: 941px;"> 
       <asp:Panel runat="server" ID="Panel_Users" ScrollBars="Auto" Style="z-index: 1; left: 748px; 
        top: 621px; position: absolute; height: 250px; width: 287px"> 
        <asp:UpdatePanel UpdateMode="Conditional" ID="UpdatePanel1" runat="server"> 
         <ContentTemplate> 
          <asp:GridView ID="Grid_UserTable" runat="server" Style="z-index: 1; left: 2px; top: 5px; 
           position: absolute; height: 152px; width: 243px" BorderColor="#666666" AutoGenerateColumns="False" 
           OnRowDataBound="MyGrid_RowDataBound"> 
           <Columns> 
            <asp:TemplateField HeaderText="Status"> 
             <ItemTemplate> 
              <asp:Image ID="Status" runat="server" /> 
             </ItemTemplate> 
            </asp:TemplateField> 
            <asp:BoundField DataField="TimeReceived" HeaderText="TimeReceived" InsertVisible="False" 
             ReadOnly="True" SortExpression="TimeReceived" /> 
            <asp:BoundField DataField="TimeRead" HeaderText="TimeRead" SortExpression="TimeRead" /> 
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> 
           </Columns> 
          </asp:GridView> 
         </ContentTemplate> 
        </asp:UpdatePanel> 
       </asp:Panel> 
      </div> 

我試圖使頁面入住當頁面每5秒刷新一次並且頁面翻到頂部時,位置相同。我試過頁面MaintainScrollPositionOnPostback =「true」。它沒有工作,我嘗試使用Ajax,但不知道如何使用它。有人可以幫助我如何用Ajax做到這一點。

回答

4

MaintainScrollPositionOnPostback只適用於IE。要做到這一點,你可以滾動你自己的客戶端腳本,或在頁面/表單的不同部分使用錨鏈接。這裏

類似的問題:

MaintainScrollPositionOnPostback is not working - how to debug?

MaintainScrollPositionOnPostback not working with javascript:__doPostBack

maintainScrollPositionOnPostback="true" does not work globally after setting in web.config ,but works in page level,what should I do?

+0

剛剛測試了MaintainScrollPositionOnPostback頁面指令,它目前在Firefox 52,Edge 38和Chrome 57中工作。 – Mike

+1

不推薦:http://www.ryadel.com/en/asp-net-mantainscrollpositiononpostback-chrome-recent -browsers/ – IrishChieftain

2

什麼聽起來像可怕的UI(頁面刷新,每5秒),一個廉價的解決將是加 '#'以及您想要在地址欄中查看網址的元素的ID,但會自動滾動到ID鏈接元素的頂部。

如果這是一個商業產品,並且你很匆忙,我建議查看JQuery的ajax實現並完全刪除這些重新加載。

可能像一條線一樣簡單:

$.ajax(
    { 
     url:"/thisPath/requestPath", 
     complete:function(data){ 
     //apply data (the http-response) to HTML 
    } 
); 

如果看起來匪夷所思你,它只是對象AJAX方法的對象字面被送入JQuery的。分配給'complete'的函數在接收到http響應時觸發,並將其作爲在.ajax方法內部建立的參數'data'提供給函數。

2

從性能的角度來看,UpdatePanels是可怕的。我會用jQuery來做這件事,並完全避免回傳。

$.ajax({ 
    url: "/path/to/url/that/returns/users", 
    type: "POST", 
    dataType: "json", 
    data: {}, 
    success: function(data, status, xhttp) 
    { 
     var html = "<table>"; 
     for (var i = 0; i < data.length; i++) 
     { 
      html += "<tr>"; 
      html += "<td></td>"; // build up table cells 
      html += "</tr>"; 
     } 
     html += "</table>"; 
     $("#NameOfDivToPutTableIn").html(html); 
    } 

}); 

如果這是一個選項,設置的URL基於本教程閱讀:

http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

如果你不想使用jquery,你仍然可以使用MS AJAX ,跳過那些更新面板。 http://www.geekzilla.co.uk/View7B75C93E-C8C9-4576-972B-2C3138DFC671.htm

1

<%@ Page MaintainScrollPositionOnPostback="true" %>作爲Plage的聲明將保留滾動位置,因爲它是

+1

對不起,只是剛剛看了一下,說你已經試過了這個:) – Spike

+0

這個方法在'Page_Load'方法中很完美!謝謝......'Page.MaintainScrollPositionOnPostBack = true;' – Pierre

19

嘗試在你的設計page..It工作正常,我下面的代碼..

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="frmName.aspx.vb" Inherits="frmName" MaintainScrollPositionOnPostBack = "true" %> 
+0

當答案像這樣簡單時,它總是很好:) – dlchambers

-3
<pages maintainScrollPositionOnPostBack="true"> 
0

嘗試這在後面的代碼中:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (Page.IsPostBack) 
    { 
     Page.MaintainScrollPositionOnPostBack = true; 
    } 
} 

Posdata:我試過用C#。

0

@{ 
 
    
 
} 
 

 
<html> 
 

 
<head> 
 
<script type="text/javascript"> 
 

 
window.onload = function() { 
 
    var div = document.getElementById("dvScroll"); 
 
    var div_position = document.getElementById("div_position"); 
 
    var position = parseInt(@Request.Form("div_position")); 
 
    if (isNaN(position)) { 
 
     position = 0; 
 
    } 
 

 
    div.scrollTop = position; 
 
    div.onscroll = function() { 
 
     div_position.value = div.scrollTop; 
 
    }; 
 
}; 
 

 
</script> 
 
</head> 
 

 
<body> 
 

 
<div id="dvScroll" style="overflow-y: scroll; height: 260px; width: 300px"> 
 
    1. This is a sample text 
 
    <br /> 
 
    2. This is a sample text 
 
    <br /> 
 
    3. This is a sample text 
 
    <br /> 
 
    4. This is a sample text 
 
    <br /> 
 
    5. This is a sample text 
 
    <br /> 
 
    6. This is a sample text 
 
    <br /> 
 
    7. This is a sample text 
 
    <br /> 
 
    8. This is a sample text 
 
    <br /> 
 
    9. This is a sample text 
 
    <br /> 
 
    10. This is a sample text 
 
    <br /> 
 
    11. This is a sample text 
 
    <br /> 
 
    12. This is a sample text 
 
    <br /> 
 
    13. This is a sample text 
 
    <br /> 
 
    14. This is a sample text 
 
    <br /> 
 
    15. This is a sample text 
 
    <br /> 
 
    16. This is a sample text 
 
    <br /> 
 
    17. This is a sample text 
 
    <br /> 
 
    18. This is a sample text 
 
    <br /> 
 
    19. This is a sample text 
 
    <br /> 
 
    20. This is a sample text 
 
    <br /> 
 
    21. This is a sample text 
 
    <br /> 
 
    22. This is a sample text 
 
    <br /> 
 
    23. This is a sample text 
 
    <br /> 
 
    24. This is a sample text 
 
    <br /> 
 
    25. This is a sample text 
 
    <br /> 
 
</div> 
 

 
<hr /> 
 
<form method="post"> 
 
<input type="hidden" id="div_position" name="div_position" /> 
 
<input type="submit" value="Cool" /> 
 
</form> 
 
</body> 
 
</html>

您可以用它來保持回發後滾動位置。

來源:http://www.aspsnippets.com/Articles/Maintain-Scroll-Position-of-DIV-on-PostBack-in-ASPNet.aspx

0

別人用這種掙扎。最簡單的解決方案是保持整個窗口的滾動位置

var xPos, yPos; 

    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function (evt, args) { 
     window.scrollTo(xPos , yPos); 
    }); 


    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(function (evt, args) { 
     xPos = $(window).scrollLeft(); 
     yPos = $(window).scrollTop(); 

    }); 

廣告開始和結束請求。開始請求使用Jquery獲取窗口滾動位置。在結束請求時,請滾動到該位置。

0

添加此行

<%@ Page MaintainScrollPositionOnPostback="true" %> 
0

有答案全網解決這個問題,並親自他們沒有工作,爲我的Firefox將試圖恢復以前的滾動位置(錯誤地),觸發window.scroll事件,它會覆蓋隱藏字段的錯誤位置,然後我的scrollTo會讀取它。 (我有Gridbacks來自回發,然後自動摺疊一些行。)

所以這裏有另一個解決這個問題的方法 - 我決定我只想在提交之後恢復滾動位置,而不是刷新,所以這是充足:

ASPX頁面:

<form runat="server" onsubmit="$('#hfScroll').val($(window).scrollTop()); return true;"> 
    <input type="hidden" id="hfScroll" value="0" /> 

的Javascript:

function restoreScroll() 
{ 
    var position = parseInt($('#hfScroll').val()); 
    if (!isNaN(position)) { 
     $(document).scrollTop(position); 
    } 
}; 
var prm = Sys.WebForms.PageRequestManager.getInstance(); 
prm.add_pageLoaded(restoreScroll); 

出於某種原因,在瀏覽器刷新我的隱藏輸入不重置爲零,所以這確實有時表現奇怪。我很想知道這是怎麼回事,我認爲它是Firefox,因爲它不會發生在IE上,但是生活太短了[他說......下載了一半的互聯網,並花了數小時在這..]。

相關問題