我正在尝试通过计时器更新gridview,因此页面本身不会发生超时。在这种情况下,我尝试执行ping操作,每隔几秒钟执行一次,然后将结果添加到gridview。
我要管理的只是2行gridview(因此最多2个条目)。所有其他内容均已“刷新”。我尝试的是下面的内容(当前版本)。我什至尝试将其添加到除了gridview之外的其他计时器中……或在放入计时器时将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?
答案 0 :(得分:0)
如果使用计时器将某些内容“添加”到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>