1、在页面没有使用AJAX时,在页面的Page命令中增加MaintainScrollPositionOnPostback=
"true"属性
<%@ Page Language="C#" AutoEventWireup="true" MaintainScrollPositionOnPostback="true" CodeBehind="Default.aspx.cs"
2、当页面没有使用AJAX时,第一种方法会无效,此时可以在DIV滚动时记住滚动条位置,并在提交后恢复滚动条位置
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" MaintainScrollPositionOnPostback="true" CodeBehind="Default.aspx.cs" Inherits="Gridview_Scroll._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> <script type="text/javascript" language="javascript"> function RecordPostion(obj) { var div1 = obj; var sx = document.getElementById(‘dvscrollX‘); var sy = document.getElementById(‘dvscrollY‘); sy.value = div1.scrollTop; sx.value = div1.scrollLeft; } function GetResultFromServer() { var sx = document.getElementById(‘dvscrollX‘); var sy = document.getElementById(‘dvscrollY‘); document.getElementById(‘dvGridView‘).scrollTop = sy.value; document.getElementById(‘dvGridView‘).scrollLeft = sx.value; } </script> </head> <body style="height:1500px"> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" > </asp:ScriptManager> <div> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <div id="dvGridView" style="height:400px; overflow:scroll" onscroll="javascript:RecordPostion(this);"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowHeader="false" > <Columns> <asp:TemplateField> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text=‘<%#Eval("Id") %>‘></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text=‘<%#Eval("Name") %>‘></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="2000"> </asp:Timer> </div> <asp:HiddenField ID="dvscrollX" runat="server" /> <asp:HiddenField ID="dvscrollY" runat="server" /> </ContentTemplate> </asp:UpdatePanel> </div> </form> </body> </html>
后台代码:
using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; namespace Gridview_Scroll { public partial class _Default : System.Web.UI.Page { protected static DataTable dt1; protected static DataTable dt2; protected static int iFlag = 0; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { dt1 = new DataTable(); dt2 = new DataTable(); dt1.Columns.Add("Id", typeof(string)); dt1.Columns.Add("Name", typeof(string)); dt2.Columns.Add("Id", typeof(string)); dt2.Columns.Add("Name", typeof(string)); for (int i = 0; i < 100; i++) { DataRow dr1 = dt1.NewRow(); dr1["Id"] = i.ToString(); dr1["Name"] = "Jack_" + i.ToString(); dt1.Rows.Add(dr1); } for (int j = 0; j < 200; j++) { DataRow dr2 = dt2.NewRow(); dr2["Id"] = j.ToString(); dr2["Name"] = "Tom_" + j.ToString(); dt2.Rows.Add(dr2); } } } private void BindGridView() { if (iFlag % 2 == 1) { this.GridView1.DataSource = dt1; } else { this.GridView1.DataSource = dt2; } this.GridView1.DataBind(); string sjs = "GetResultFromServer();"; ScriptManager.RegisterClientScriptBlock(this.GridView1, this.GetType(), "", sjs, true); } protected void Timer1_Tick(object sender, EventArgs e) { iFlag++; BindGridView(); } } }
这两种方法,摘自CSDN两位网友Mr_ALei和AngleProgrammer在CSDN论坛中的回复内容
时间: 2024-10-22 02:50:46