我正在使用 ASP.NET WebForms 实现跨浏览器的 Web 应用程序。因此,我动态创建了表,其中每个单元格都是从 TableCell
继承的自定义用户控件。在我的控件中有一个带有几个文本框的 asp:Panel
。由于表格是动态创建的,它可以是不同的宽度。当我更改其中一个文本框中的文本时,TextChanged
事件会触发并刷新页面。但是因为它滚动跳转到顶部。如何将页面滚动到上一个位置?
我试过 Page.MaintainScrollPositionOnPostBack = true
但它不起作用。
谢谢!
最佳答案
您将需要使用 JavaScript 来协助您完成这项工作。几年前,我遇到了同样的问题,并且能够在回发后使用 JavaScript 进行滚动来解决。
这里有一篇文章供您阅读:http://basgun.wordpress.com/2008/06/09/maintain-scroll-position-updatepanel-postback/
基本上,您将向您的页面请求添加事件和参数,这将为您完成提升。
文章中的代码:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" ScriptMode="Release" />
<script type="text/javascript">
// It is important to place this JavaScript code after ScriptManager1
var xPos, yPos;
var prm = Sys.WebForms.PageRequestManager.getInstance();
function BeginRequestHandler(sender, args) {
if ($get('<%=Panel1.ClientID%>') != null) {
// Get X and Y positions of scrollbar before the partial postback
xPos = $get('<%=Panel1.ClientID%>').scrollLeft;
yPos = $get('<%=Panel1.ClientID%>').scrollTop;
}
}
function EndRequestHandler(sender, args) {
if ($get('<%=Panel1.ClientID%>') != null) {
// Set X and Y positions back to the scrollbar
// after partial postback
$get('<%=Panel1.ClientID%>').scrollLeft = xPos;
$get('<%=Panel1.ClientID%>').scrollTop = yPos;
}
}
prm.add_beginRequest(BeginRequestHandler);
prm.add_endRequest(EndRequestHandler);
</script>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server" Height="300">
<%-- Some stuff which would cause a partial postback goes here --%>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</form>
关于c# - 在 ASP.NET WebForms 中回发后滚动到上一个位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21121119/