且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET AJAX UpdatePanel滚动问题

更新时间:2023-01-06 22:21:40

尝试使用以下三种方法之一维护 MaintainScrollPositionOnPostback

  • 以编程方式-Page.MaintainScrollPositionOnPostBack = true;
  • 页面声明-<%@页面MaintenanceScrollPositionOnPostback ="true"%>
  • 在web.config中-<页面maintenanceScrollPositionOnPostBack ="true"/>

您可能还需要在scriptmanager声明之后添加以下javascript:

<script type="text/javascript">

var prm = Sys.WebForms.PageRequestManager.getInstance();

prm.add_beginRequest(beginRequest);

function beginRequest()
{
    prm._scrollPosition = null;
}

</script> 

I'll try and be concise:

  1. I have a dropdownlist with Autopostback set to true
  2. I have an UpdatePanel that contains a Label.
  3. When the downdownlist selection is changed, I want to update the label.

Problem: Focus is lost on the dropdownlist, forcing the user to click on the dropdownlist to reset focus back to the control.

My "solution": In the DropDownList_SelectionChanged event, set focus back to the drop down list:

dropdownlist1.focus()

While this works great in IE, Firefox and Chrome change the scroll position such that the control which was assigned focus is positioned at the bottom on the visible portion of the browser window. This is often a very disorientating side effect.

How can this be avoided so it works in FF as it does in IE?

Try MaintainScrollPositionOnPostback in one of these 3 ways

  • Programmatically - Page.MaintainScrollPositionOnPostBack = true;
  • Page declaration - <%@ Page MaintainScrollPositionOnPostback="true" %>
  • In the web.config - <pages maintainScrollPositionOnPostBack="true" />

You may also need to add this javascript after the scriptmanager declaration:

<script type="text/javascript">

var prm = Sys.WebForms.PageRequestManager.getInstance();

prm.add_beginRequest(beginRequest);

function beginRequest()
{
    prm._scrollPosition = null;
}

</script>