我创建了一个列表,该列表基于此 page 中的示例 1 生成自定义事件,只要列表中有任何新元素,我就需要更新一个 aspx 页面。
当我调试应用程序时,我可以看到该值已更新,但页面上没有显示任何内容。
ASPX
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<fieldset>
<legend>UpdatePanel</legend>
<asp:Label ID="xpto" runat="server" Text="zzzzzzzzzzzz"></asp:Label>
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
代码隐藏
MessageHandling.DashboardRequests.Changed += new EventHandler(ListChanged);
...
...
...
private void ListChanged(object sender, EventArgs e)
{
DateTime dt = DateTime.Now;
xpto.Text = dt.ToString();
}
编辑:
如果我将 UpdateMode 更改为 Always 并将 ListChanged 方法更改为:
private void ListChanged(object sender, EventArgs e)
{
DateTime dt = DateTime.Now;
xpto.Text = dt.ToString();
UpdatePanel1.Update();
}
我收到以下错误:
The Update method can only be called on UpdatePanel with ID 'UpdatePanel1' when UpdateMode is set to Conditional.
如果我将 UpdateMode 设置为 Conditional,则不会再发生任何事情。
如果我创建一个定时器并添加这个方法:
protected void Timer1_Tick(object sender, EventArgs e)
{
DateTime dt = DateTime.Now;
xpto.Text = dt.ToString();
}
xpto 在计时器方法中正确更新
最佳答案
按照 Ates 的建议将 UpdateMode
更改为 Always
。
(旧)
代码看起来不错,所以我会做一个 WAG -- 你在页面生命周期的其他地方更新 xpto.Text
任何地方吗?
关于c# - 使用自定义事件刷新 ASP.NET 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8271277/