在使用以下编码将焦点设置为跨多个浏览器的回发控件后,我试图保持滚动位置。在 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>