0

So there's a ListView element on my ASP.NET page that I need to be able to update from the code behind. To my understanding, Microsoft has prepared UpdatePanels and DataBindung for exactly such purpose, allowing me to "bind" the ListView's content to a property member in the code behind and promising to take care of updating the browser's view automatically (?) when the property changes. However, only the initial load of items via GetStuff() works; I can see in the debug console that my timer keeps adding new elements to the List, but those never arrive in the browser's view. What am I missing?

In 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>

And in 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 Answer 1

1

System.Timers.Timer does not work with a webpage. The reason that t is disposed after the page is sent to the client. Use a Timer control if you really want to use one.

<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick"></asp:Timer>

And then you can update the ListView in Timer1_Tick.

protected void Timer1_Tick(object sender, EventArgs e)
{
    //update the ListView
}

Place the Timer Control inside the UPdatePanel if you do not want a full postback when the timer is triggered.

Another thing to remember is that although you use an UpdatePanel, a complete page life cycle is triggered. So all other code you use in Page Load (and PrerRender) is executed even when only the ListView update is visible to the user. This could put a huge load on the server when an updatepanel is triggered every few seconds. Maybe better use Ajax.

PS you don't need to use Page_PreRender to bind data.

Sign up to request clarification or add additional context in comments.

1 Comment

I'd never thought this was the issue! My timer reliably printed messages to the debug console as expected, but nothing else had happened. Thanks a lot, especially for the hint on page life cycles!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.