在ASP.NET中回发后,维护滚动位置有很多类似的帖子。我已经尝试了其中几个没有成功,但是,很多人似乎已经成功使用以下Javascript。我也尝试过使用.scrollLeft和.offsetLeft,但没有任何运气。
var xPos, yPos;
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_beginRequest(BeginRequestHandler);
prm.add_endRequest(EndRequestHandler);
function BeginRequestHandler(sender, args) {
alert("Begin Request?");
if ($get('<%=PanelReportInput.ClientID%>') != null) {
xPos = $get('<%=PanelReportInput.ClientID%>').offsetLeft;
yPos = $get('<%=PanelReportInput.ClientID%>').scrollTop;
}
}
function EndRequestHandler(sender, args) {
alert("End Request?");
if ($get('<%=PanelReportInput.ClientID%>') != null) {
$get('<%=PanelReportInput.ClientID%>').offsetLeft = xPos;
$get('<%=PanelReportInput.ClientID%>').scrollTop = yPos;
}
}
我在一个名为ScrollPosition.js的文件中有上面的javascript,它被拉入我的母版页面。 我同时收到警报,但是当我调试此代码时,xPos和yPos的值都为&#39; undefined&#39;。下面是我的.aspx页面,但在上面的JavaScript代码中,我还尝试使用&#39;&lt;%= UpdatePanelMain.ClientID%&gt;&#39;以及&#39;&lt;%= DailyStoreListView.ClientID%&gt;&#39;和&#39;&lt;%= DailyStoreListView.ClientID%&gt;&#39;。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DailyOrderSchedule.aspx.cs" MasterPageFile="~/MasterPages/Main.Master"
Inherits="OfficeIntranet.Forms.Warehouse.DailyOrdersSchedule" MaintainScrollPositionOnPostback="true" %>
<%@ Register TagPrefix="uc" TagName="DateStoreSelectoruc" Src="../../Controls/Warehouse/DateStoreSelector.ascx" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<title>Order Schedule</title>
<link href="../../Styles/StyleSheet.css" rel="stylesheet" type="text/css" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolderPageHeading" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolderBody" runat="server">
<div id="headerE" runat="server" style="text-align: center; width: auto;">
<asp:Label ID="MainH" CssClass="headerText" runat="server">Order Schedule</asp:Label>
<asp:Label ID="headVersion" CssClass="textarea" runat="server"></asp:Label>
</div>
<asp:UpdatePanel ID="UpdatePanelMain" runat="server">
<ContentTemplate>
<asp:Panel runat="server" ID="PanelReportInput" Width="980px" BorderColor="#CCCCCC" BorderStyle="Inset" BorderWidth="0px" Style="padding: 10px; text-align: center;">
<asp:ListView ID="DailyStoreListView" runat="server" GroupItemCount="5" GroupPlaceholderID="GPH" style="width:940px"
ItemPlaceholderID="itemPlaceholder" OnItemDataBound="lv_ItemDataBound" >
<LayoutTemplate>
<div runat="server" style="padding: 2px; width: 932px; height:740px; overflow:scroll">
<div id="CalendarDays" style="padding: 2px">
<div class="SPAWeekFiller5"> </div>
<div class="SPAWeekDays" style="margin-right: 2px">Friday</div>
<div class="SPAWeekFiller"> </div>
<div class="SPAWeekDays">Thursday</div>
<div class="SPAWeekFiller"> </div>
<div class="SPAWeekDays">Wednesday</div>
<div class="SPAWeekFiller"> </div>
<div class="SPAWeekDays">Tuesday</div>
<div class="SPAWeekFiller"> </div>
<div class="SPAWeekDays">Monday</div>
<div class="SPAWeekFiller"> </div>
<div style="clear: both; padding: -1px;"></div>
</div>
<div id="groupPlaceholder" runat="server"></div>
</div>
</LayoutTemplate>
<GroupTemplate>
<div style="padding: 6px 0px 6px 6px; width: 910px;" id="GTdIV">
<div id="itemPlaceholder" runat="server"></div>
<div style="clear: both; padding: 4px;"></div>
</div>
</GroupTemplate>
<ItemTemplate >
<uc:DateStoreSelectoruc ID="DateStoreSelectorControl" runat="server" />
</ItemTemplate>
<EmptyItemTemplate>
<div style="padding: 5px; height: 220px; width: 170px; float: left;">
</div>
</EmptyItemTemplate>
<EmptyDataTemplate>
<div style="padding: 10px; background: #CACACA; width: 930px;">
No Records Found.
</div>
</EmptyDataTemplate>
</asp:ListView>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
这是Master Page
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Main.master.cs" Inherits="OfficeIntranet.MasterPages.Main" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>My Page</title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="scriptManager" runat="server" EnablePartialRendering="True" >
<Scripts>
<asp:ScriptReference Path="../Scripts/ScrollPosition.js" />
</Scripts>
</asp:ScriptManager>
<asp:Panel ID="wrapperP" runat="server">
<div id="art-page-background-simple-gradient"></div>
<div id="art-page-background-glare">
<div id="art-page-background-glare-image"></div>
...
如果有人对我做错了什么有任何想法,我将非常感谢任何帮助/建议!
答案 0 :(得分:1)
您的Javascript是否在单独的文件中?
在这种情况下,Javascript中<%=something%>
形式的标签不会被您想要的实际值替换。这仅适用于您的Javascript是内联的,页面的一部分。
这是因为<% %>
标记在将Web页面发送到浏览器之前由Web服务器上的ASP.NET编译器解释。如您所知,标签将由实际评估值替换。外部Javascript文件永远不会被Web服务器处理:它在解释处理过的页面时被浏览器拉入。