因此我的ASP.NET页面上有一个ListView元素,我需要能够从后面的代码更新。根据我的理解,微软已经为这个目的准备了UpdatePanels和DataBindung,让我能够"绑定" ListView的内容提供给后面代码中的属性成员,并承诺在属性更改时自动更新浏览器的视图(?)。
但是,只有通过GetStuff()
的项目的初始加载有效;我可以在调试控制台中看到我的计时器不断向List添加新元素,但这些元素永远不会到达浏览器的视图。我错过了什么?
在Default.aspx中:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="myLittleProject.Default" %>
<%@ Register Src="~/StuffListItemControl.ascx" TagPrefix="stf" TagName="StuffListItem" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<!-- some irrelevant stuff -->
</head>
<bod>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager" runat="server"></asp:ScriptManager>
<!-- some irrelevant stuff -->
<asp:UpdatePanel runat="server" ID="StuffUpdatePanel" UpdateMode="Always">
<ContentTemplate>
<ul>
<asp:ListView ID="StuffBoundContent" runat="server">
<ItemTemplate>
<stf:StuffListItem runat="server" ID="StuffListItemControl" />
</ItemTemplate>
</asp:ListView>
</ul>
</ContentTemplate>
</asp:UpdatePanel>
<!-- some more irrelevant stuff -->
</form>
</body>
</html>
在Default.aspx.cs中:
using System.Collections.Generic;
namespace myLittleProject
{
public partial class Default : System.Web.UI.Page
{
public static List<Stuff> StuffContent;
protected void Page_Load(object sender, EventArgs e)
{
StuffContent = Stuff.GetStuff(); // returns a list of three elements from the database
System.Timers.Timer t = new System.Timers.Timer();
t.Interval = 3000;
t.Elapsed += T_Tick;
t.Enabled = true;
}
protected void Page_PreRender(object sender, EventArgs e)
{
StuffBoundContent.DataSource = StuffContent;
StuffBoundContent.DataBind();
}
private void T_Tick(object sender, EventArgs e)
{
StuffContent.Add(new Stuff(StuffContent.Count + 1, DateTime.Now.ToShortDateString(), new string[] { "what", "ever" }));
System.Diagnostics.Debug.WriteLine("[TIMER EVENT] StuffContent.Count = " + StuffContent.Count.ToString());
}
}
}
答案 0 :(得分:1)
System.Timers.Timer
不适用于网页。在页面发送到客户端之后处理t
的原因。如果你真的想使用它,请使用Timer控件。
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick"></asp:Timer>
然后您可以更新Timer1_Tick
中的ListView。
protected void Timer1_Tick(object sender, EventArgs e)
{
//update the ListView
}
如果在触发计时器时不想要完整的回发,请将Timer Control放在UPdatePanel内。
要记住的另一件事是,虽然您使用UpdatePanel,但会触发完整的页面生命周期。因此,即使只有ListView更新对用户可见,您在Page Load(和PrerRender)中使用的所有其他代码也会被执行。每隔几秒触发一次更新通道时,这可能会给服务器带来巨大负担。也许更好地使用Ajax。
PS您不需要使用Page_PreRender
来绑定数据。