C# 在ASP.NET WebForms中回发后滚动到上一个位置
我正在用ASP.NET WebForms实现跨浏览器web应用程序。所以,我动态创建了表,其中每个单元格都是从C# 在ASP.NET WebForms中回发后滚动到上一个位置,c#,javascript,jquery,asp.net,webforms,C#,Javascript,Jquery,Asp.net,Webforms,我正在用ASP.NET WebForms实现跨浏览器web应用程序。所以,我动态创建了表,其中每个单元格都是从TableCell继承的自定义用户控件。在我的控件中,有带有多个文本框的asp:Panel。由于表的动态创建,它可以是不同的宽度。当我更改其中一个文本框中的文本时,TextChanged事件触发并刷新页面。但正因为如此,滚动到顶部。如何将页面滚动到上一个位置 我尝试了Page.maintaintScrollPositionOnPostback=true,但没有成功 谢谢大家! 您需要使用
TableCell
继承的自定义用户控件。在我的控件中,有带有多个文本框的asp:Panel
。由于表的动态创建,它可以是不同的宽度。当我更改其中一个文本框中的文本时,TextChanged
事件触发并刷新页面。但正因为如此,滚动到顶部。如何将页面滚动到上一个位置
我尝试了Page.maintaintScrollPositionOnPostback=true
,但没有成功
谢谢大家! 您需要使用JavaScript来帮助您完成这项工作。几年前,我遇到了同样的问题,并且能够在发帖后使用JavaScript进行滚动 这里有一篇文章供您阅读: 本质上,您将向页面请求添加事件和参数,这将为您起到提升作用 文章中的代码:
<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>
//将此JavaScript代码放在ScriptManager1之后很重要
var xPos,yPos;
var prm=Sys.WebForms.PageRequestManager.getInstance();
函数BeginRequestHandler(发送方,参数){
如果($get(“”)!=null){
//在部分回发之前获取滚动条的X和Y位置
xPos=$get(“”).scrollLeft;
yPos=$get(“”).scrollTop;
}
}
函数EndRequestHandler(发送方,参数){
如果($get(“”)!=null){
//将X和Y位置设置回滚动条
//部分回发后
$get(“”).scrollLeft=xPos;
$get(“”).scrollTop=yPos;
}
}
prm.add_beginRequest(BeginRequestHandler);
prm.add_endRequest(EndRequestHandler);