【问题标题】:ASP.net gridview, autorefresh, txt as source and maintain scroll positionASP.net gridview,自动刷新,文本作为源并保持滚动位置
【发布时间】:2014-08-10 05:26:06
【问题描述】:

我希望这个 gridview 模拟一个控制台,我拥有的文本文件是一个服务器控制台日志。

我希望它:

*文件更改时刷新。(因此是计时器)

*滚动条位置在更新时保持不变,除非在底部。

*当滚动条位置在底部时,数据绑定更新后保持在底部。

当前问题:在计时器上,databind() 将滚动条位置移回顶部,并且经常刷新,您甚至根本无法向下滚动。

这是我当前的代码。感谢您的帮助!

asp.net

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TEST CONSOLE.aspx.cs" Inherits="WebApplication1.TEST_CONSOLE" %>
<!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 runat="server">
</head>  
<body>  
<form id="form1" runat="server">  
<div>  
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:Timer ID="Timer1" runat="server" Interval="30" ontick="Timer1_Tick"></asp:Timer>
            <div style="width: 100%; height: 400px; overflow: scroll">
                <asp:GridView ID="GridView1" runat="server" MaintainScrollPositionOnPostback="true">  
                </asp:GridView>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>  
</div>  
</form>  
</body>  
</html>

c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace WebApplication1
{
public partial class TEST_CONSOLE : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    StreamReader objStreamReader = new StreamReader(Server.MapPath("TextFile.txt"));
    var arrText = new List<string>();
   // ' Loop through the file and add each line to the ArrayList  
    while( objStreamReader.Peek() >= 0){
        arrText.Add(objStreamReader.ReadLine());
    }

    //' Close the reader  
    objStreamReader.Close();

    //' Bind the results to the GridView  
    GridView1.DataSource = arrText;
    GridView1.DataBind();
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        GridView1.DataBind();


    }
}
}

【问题讨论】:

  • 您遇到的问题和问题/问题在哪里?
  • 添加了发布问题。
  • 在这里找到答案forums.asp.net/t/…

标签: c# asp.net gridview scrollbar refresh


【解决方案1】:

在这里找到大部分答案 http://forums.asp.net/t/1220070.aspx?how+to+maintain+scroll+position+in+gridview+or+datagrid+during+postback

使用ajax和这两个函数

    function Func() {//document.getElementById("hdnScrollTop").value
    document.getElementById("divScroll").scrollTop = document.getElementById("hdnScrollTop").value;
}
function Func2() {
    var s = document.getElementById("divScroll").scrollTop;

    document.getElementById('hdnScrollTop').value = s;

【讨论】:

    猜你喜欢
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 2011-07-31
    • 1970-01-01
    • 2020-02-29
    • 2015-04-23
    • 1970-01-01
    相关资源
    最近更新 更多