c# - 使用自定义事件刷新 ASP.NET 页面

标签 c# asp.net ajax events

我创建了一个列表,该列表基于此 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/

相关文章:

c# - c#中显式接口(interface)实现和隐式接口(interface)实现有什么区别

ASP.NET 下拉列表 - 在 MasterPage EnableViewState = true 时工作

asp.net - 检测 SQL 的非关闭连接

javascript - 停止在 Handlebars 中渲染模板

c# - 更新局部区域 MVC3

c# - C# 如何使用#if 预处理器指令编译代码?

c# - MVC3 (Razor) 单选按钮检查

c# - 将事件处理程序附加到 mshtml.DispHTMLInputElement

javascript - jQuery 的 .on 函数似乎不喜欢 'load' 事件

c# - 相同的情况