Gonçalo Cardoso
Gonçalo Cardoso

Reputation: 2262

Refresh ASP.NET page using custom event

I have created a List which generates a custom event based on Example 1 from this page, and I need to update an aspx page whenever there are any new elements in the List.

When I debug the application I can see that the value was updated, but nothing appears on the page.

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>

Code Behind

MessageHandling.DashboardRequests.Changed += new EventHandler(ListChanged);
...
...
...
private void ListChanged(object sender, EventArgs e)
{
    DateTime dt = DateTime.Now;
    xpto.Text = dt.ToString();
}

EDIT:

If I change the UpdateMode to Always and ListChanged method to:

private void ListChanged(object sender, EventArgs e)
{
  DateTime dt = DateTime.Now;
  xpto.Text = dt.ToString();
  UpdatePanel1.Update();
} 

I get the following error:

The Update method can only be called on UpdatePanel with ID 'UpdatePanel1' when UpdateMode is set to Conditional. 

And if I set the UpdateMode to Conditional nothing happens again.

If I create a timer and add this method:

protected void Timer1_Tick(object sender, EventArgs e)
{
  DateTime dt = DateTime.Now;
  xpto.Text = dt.ToString();
}

the xpto is updated in the timer method correctly

Upvotes: 2

Views: 2007

Answers (1)

Hogan
Hogan

Reputation: 70523

Change UpdateMode to Always as suggested by Ates.


(old)

The code looks ok, so I will make a WAG -- are you updating xpto.Text ANYWHERE else in the page lifecycle?

Upvotes: 1

Related Questions