我遇到一个问题,我有一个 UpdatePanel,它使用计时器来触发用新点更新 ASP 图表(本质上是位于https://web.archive.org/web/20201205213920/https://www.4guysfromrolla.com/articles/121609-1.aspx https://web.archive.org/web/20201205213920/https://www.4guysfromrolla.com/articles/121609-1.aspx在“创建实时图表”下)。我遇到一个问题,每当计时器滴答作响时,整个页面就会滚动到顶部。当计时器计时时,如何保持页面中的滚动位置?我已经尝试过以下说明https://web.archive.org/web/20211020140248/https://www.4guysfromrolla.com/articles/111704-1.aspx https://web.archive.org/web/20211020140248/https://www.4guysfromrolla.com/articles/111704-1.aspx和其他几个类似的 JavaScript 解决方案,但是每当计时器滴答作响时,x 和 y 变量就会被清除。
Code:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication4._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"></asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:ScriptManager ID="scmManager" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="updRealtimeChart" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:Chart ID="chtRandomData" ...></asp:Chart><br />
<asp:Repeater ID="valueRepeater"...></asp:Repeater>
<asp:Label ID="errorLabel" Font-Bold="true" Font-Size="Larger" ForeColor="Firebrick" BackColor="Khaki" runat="server"></asp:Label>
<asp:Timer ID="tmrRefreshChart" runat="server" Interval="300"></asp:Timer>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="periodUpdate" />
</Triggers>
</asp:UpdatePanel>
...etc rest of the page...
Edit:在后面的代码中,我尝试这样做来检查发生了什么:
public partial class _Default : System.Web.UI.Page
{
public int count = 0;
protected void Page_Init(object sender, EventArgs e)
{
Page.MaintainScrollPositionOnPostBack = true;
count++;
}
protected void Page_Load(object sender, EventArgs e)
{
errorLabel.Text += count;
... }
当我运行此命令时,UpdatePanel 不断向 errorLabel 添加“1”,表明 Page_Load 和 Page_Init 函数仅发生一次,但更新面板仍在移动滚动位置。
您可以在 javascript 中使用 document.body.scrollTop 来存储滚动位置,然后将其分配回来。我用过它,它对我有用。
也尝试一下
Page.MaintainScrollPositionOnPostBack = true;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)