2013-08-29 160 views
0

後在Chrome和Safari TabPanel中保持滾動位置我試圖在使用以下編碼設置焦點以跨多個瀏覽器回發控件後保持滾動位置。在IE中可以正常工作,但在Chrome,Firefox和Safari中,當我嘗試重新設置導致回發的控件時,滾動會跳回頂端。我使用scriptmanager.setfocus(control)方法設置焦點。 注意:我指的是選項卡面板中的垂直滾動條,而不是主頁滾動條。如何在設置焦點控制

Private Sub Page_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender 
    Dim PostControl As Control = FindControlById(HiddenFieldPostControl.Value) 
     If PostControl IsNot Nothing Then 
      Dim sm As ScriptManager = ScriptManager.GetCurrent(Master.Page) 
      sm.SetFocus(PostControl) 
     End If 
    End If 
End Sub 

//-----------------------------------------------------------------------------------// 
// Maintain scroll position in given element or control 
//-----------------------------------------------------------------------------------// 
var yPos 
var prm = Sys.WebForms.PageRequestManager.getInstance(); 
prm.add_beginRequest(BeginRequestHandler); 
prm.add_endRequest(EndRequestHandler); 

function BeginRequestHandler(sender, args) { 
    var tb = document.getElementById('MainContent_RightTabContainer_InputTabPanel'); 
    if (tb != null) { 
     yPos= $get('InputPanel.ClientID').scrollTop; 
    } 
} 
function EndRequestHandler(sender, args) { 
    var tb = document.getElementById('MainContent_RightTabContainer_InputTabPanel'); 
    if (tb != null) { 
     $get('InputPanel.ClientID').scrollTop = yPos; 
    } 
} 

<asp:Panel ID="InputPanel" runat="server" CssClasss="MenuPanel" EnableViewState="False"> 
    ...controls 
</asp:Panel> 

回答

0

問題通過使用postBackElement在javascript設置焦點,而不是使用ScriptManager將焦點設置在服務器端事件的解決。我發現下面的代碼在Focus Scroll Issue

這是我更新的JavaScript代碼。此外,

//-----------------------------------------------------------------------------------// 
// Maintain scroll position in given element or control 
//-----------------------------------------------------------------------------------// 
var yPos 
var prm = Sys.WebForms.PageRequestManager.getInstance(); 
prm.add_beginRequest(BeginRequestHandler); 
prm.add_endRequest(EndRequestHandler); 

function BeginRequestHandler(sender, args) { 
    var tb = document.getElementById('MainContent_RightTabContainer_InputTabPanel'); 
    if (tb != null) { 
     postBackElement = args.get_postBackElement(); 
     yPos= $get('InputPanel.ClientID').scrollTop; 
    } 
} 
function EndRequestHandler(sender, args) { 
    var tb = document.getElementById('MainContent_RightTabContainer_InputTabPanel'); 
    if (tb != null) { 
     $get('InputPanel.ClientID').scrollTop = yPos; 
     if (postBackElement != null) { 
      document.getElementById(postBackElement.id).focus(); 
     } 
    } 
} 
0

您可以修改Page指令這樣:

<%@ Page Title="" Language="C#" MaintainScrollPositionOnPostback="true" Inherits="SomeObject" %> 
+0

是的,我已經使用MaintainScrollPositionOnPostback但只適用於網頁,而不是一個tabpanel內的滾動條從Page_PreRender事件刪除sm.SetFocus(PostControl)。謝謝。 – TroyS