通过定时器更新 Gridview
Updateing Gridview Via Timer
我正在尝试通过计时器更新 gridview,因此页面本身没有超时 occurs.What 我在这种情况下尝试的是每隔几秒执行一次 ping 并添加结果到网格视图。
我管理的只是一个 2 行的网格视图(因此最多有 2 个条目)。所有其他内容是....."refreshed"。我尝试的是以下内容(当前版本)。除了 gridview 之外,我什至尝试将计时器放入 updatepanel ....或者在计时器进入时将 gridview 留在面板之外(上一个版本有计时器工作但 gridview 根本没有改变) .
隐藏代码:
public partial class Ping : System.Web.UI.Page
{
private int _MaxPings = 100;
private DataTable _PingResults = new DataTable();
private string _Url = "127.0.0.1";
protected void Page_Load(object sender, EventArgs e)
{
_PingResults.Columns.Add("Time");
_PingResults.Columns.Add("Result");
UI_PingResult.DataSource = _PingResults;
UI_PingResult.DataBind();
AddToPingResult(_Url);
}
public void AddToPingResult(string IP)
{
Process pingProcess = new Process();
int secondsWaited = 0;
ProcessStartInfo pingProcessStartInfo = new ProcessStartInfo("ping ");
if (_PingResults.Rows.Count >= _MaxPings)
{
_PingResults.Rows.RemoveAt(0);
}
pingProcessStartInfo.Arguments = "-n 1 " + IP;
pingProcessStartInfo.CreateNoWindow = true;
pingProcessStartInfo.ErrorDialog = false;
pingProcessStartInfo.RedirectStandardOutput = true;
pingProcessStartInfo.UseShellExecute = false;
pingProcess.StartInfo = pingProcessStartInfo;
pingProcess.Start();
while (!pingProcess.HasExited && secondsWaited <= 10) // 10 Seconds
{
Thread.Sleep(1000);
secondsWaited++;
}
if (pingProcess.HasExited)
{
string resultText = String.Empty;
int linesRead = 0;
while (!pingProcess.StandardOutput.EndOfStream && linesRead < 3)
{
string outputLine = pingProcess.StandardOutput.ReadLine().Trim();
if (linesRead == 2)
{
resultText += outputLine + "<br />";
}
linesRead++;
}
_PingResults.Rows.Add(DateTime.Now, resultText);
}
UI_PingResult.DataBind();
}
protected void UI_Timer_Tick(object sender, EventArgs e)
{
AddToPingResult(_Url);
}
}
Html:
<asp:Timer runat="server" ID="UI_Timer" Interval="6000" OnTick="UI_Timer_Tick"></asp:Timer>
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="UI_Timer" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:GridView ID="UI_PingResult" runat="server" Width="100%" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Time" HeaderText="Zeitpunkt" />
<asp:BoundField DataField="Result" HtmlEncode="false" HeaderText="Resultat" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
我现在的问题是:我在这里做错了什么?因此,我需要做什么才能通过计时器添加到 gridview?
如果您使用定时器来 "add" 像 GridView 这样的控件,您需要将以前的值存储在某个地方,否则在下一个定时器节拍中添加数据时它们将丢失。如果要跨页面或页面刷新保留数据,请使用 Session
而不是 ViewState
。请参阅下面的示例。
protected void Timer1_Tick(object sender, EventArgs e)
{
//create a new table to hold the data
DataTable table;
//check if the viewstate item exists and if not create a new table
if (ViewState["TimerTable"] == null)
{
table = new DataTable();
table.Columns.Add("TimeStamp", typeof(DateTime));
//create the viewstate object
ViewState["TimerTable"] = table;
}
else
{
//the viewstate exists so you can cast it back to a datatable
table = ViewState["TimerTable"] as DataTable;
}
//add the next tick data to the table
table.Rows.Add(DateTime.Now);
//rebind all the datat to the gridview
GridView1.DataSource = table;
GridView1.DataBind();
}
ASPX
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="2500"></asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
我正在尝试通过计时器更新 gridview,因此页面本身没有超时 occurs.What 我在这种情况下尝试的是每隔几秒执行一次 ping 并添加结果到网格视图。
我管理的只是一个 2 行的网格视图(因此最多有 2 个条目)。所有其他内容是....."refreshed"。我尝试的是以下内容(当前版本)。除了 gridview 之外,我什至尝试将计时器放入 updatepanel ....或者在计时器进入时将 gridview 留在面板之外(上一个版本有计时器工作但 gridview 根本没有改变) .
隐藏代码:
public partial class Ping : System.Web.UI.Page
{
private int _MaxPings = 100;
private DataTable _PingResults = new DataTable();
private string _Url = "127.0.0.1";
protected void Page_Load(object sender, EventArgs e)
{
_PingResults.Columns.Add("Time");
_PingResults.Columns.Add("Result");
UI_PingResult.DataSource = _PingResults;
UI_PingResult.DataBind();
AddToPingResult(_Url);
}
public void AddToPingResult(string IP)
{
Process pingProcess = new Process();
int secondsWaited = 0;
ProcessStartInfo pingProcessStartInfo = new ProcessStartInfo("ping ");
if (_PingResults.Rows.Count >= _MaxPings)
{
_PingResults.Rows.RemoveAt(0);
}
pingProcessStartInfo.Arguments = "-n 1 " + IP;
pingProcessStartInfo.CreateNoWindow = true;
pingProcessStartInfo.ErrorDialog = false;
pingProcessStartInfo.RedirectStandardOutput = true;
pingProcessStartInfo.UseShellExecute = false;
pingProcess.StartInfo = pingProcessStartInfo;
pingProcess.Start();
while (!pingProcess.HasExited && secondsWaited <= 10) // 10 Seconds
{
Thread.Sleep(1000);
secondsWaited++;
}
if (pingProcess.HasExited)
{
string resultText = String.Empty;
int linesRead = 0;
while (!pingProcess.StandardOutput.EndOfStream && linesRead < 3)
{
string outputLine = pingProcess.StandardOutput.ReadLine().Trim();
if (linesRead == 2)
{
resultText += outputLine + "<br />";
}
linesRead++;
}
_PingResults.Rows.Add(DateTime.Now, resultText);
}
UI_PingResult.DataBind();
}
protected void UI_Timer_Tick(object sender, EventArgs e)
{
AddToPingResult(_Url);
}
}
Html:
<asp:Timer runat="server" ID="UI_Timer" Interval="6000" OnTick="UI_Timer_Tick"></asp:Timer>
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="UI_Timer" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:GridView ID="UI_PingResult" runat="server" Width="100%" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Time" HeaderText="Zeitpunkt" />
<asp:BoundField DataField="Result" HtmlEncode="false" HeaderText="Resultat" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
我现在的问题是:我在这里做错了什么?因此,我需要做什么才能通过计时器添加到 gridview?
如果您使用定时器来 "add" 像 GridView 这样的控件,您需要将以前的值存储在某个地方,否则在下一个定时器节拍中添加数据时它们将丢失。如果要跨页面或页面刷新保留数据,请使用 Session
而不是 ViewState
。请参阅下面的示例。
protected void Timer1_Tick(object sender, EventArgs e)
{
//create a new table to hold the data
DataTable table;
//check if the viewstate item exists and if not create a new table
if (ViewState["TimerTable"] == null)
{
table = new DataTable();
table.Columns.Add("TimeStamp", typeof(DateTime));
//create the viewstate object
ViewState["TimerTable"] = table;
}
else
{
//the viewstate exists so you can cast it back to a datatable
table = ViewState["TimerTable"] as DataTable;
}
//add the next tick data to the table
table.Rows.Add(DateTime.Now);
//rebind all the datat to the gridview
GridView1.DataSource = table;
GridView1.DataBind();
}
ASPX
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="2500"></asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>