使用asp.net和母版页在ListView中保持回发时的滚动位置



在ASP.NET中,有很多类似的帖子在回发后保持滚动位置。我尝试过其中几篇,但都没有成功,但是,很多人似乎都成功地使用了下面的Javascript。我也试过使用.sollLeft和.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的值都为"undefined"。下面是我的.aspx页面,但在上面的JavaScript代码中,我也尝试过使用'<%=UpdatePanelMain.ClientID%>'以及'<%=DailyStoreListView.ClientID%>'和'<%=DailyStoreListView.ClientID%>'.

<%@ 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">&nbsp;</div>
                                <div class="SPAWeekDays" style="margin-right: 2px">Friday</div>
                                <div class="SPAWeekFiller">&nbsp;</div>
                                <div class="SPAWeekDays">Thursday</div>
                                <div class="SPAWeekFiller">&nbsp;</div>
                                <div class="SPAWeekDays">Wednesday</div>
                                <div class="SPAWeekFiller">&nbsp;</div>
                                <div class="SPAWeekDays">Tuesday</div>
                                <div class="SPAWeekFiller">&nbsp;</div>
                                <div class="SPAWeekDays">Monday</div>
                                <div class="SPAWeekFiller">&nbsp;</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 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>
    ...

如果有人对我做错了什么有任何想法,我将非常感谢任何帮助/建议!

您的Javascript在一个单独的文件中?

在这种情况下,Javascript中<%=something%>形式的标记不会被所需的实际值所取代。只有当您的Javascript是内联的,是页面的一部分时,这才有效。

这是因为<% %>标记是由web服务器上的ASP.NET编译器在将网页发送到浏览器之前进行解释的。如您所知,标记将被实际评估值所替换。外部Javascript文件从不由web服务器处理:它是由浏览器在解释处理后的页面时拉入的。

最新更新