title: How to implement real time data for a web page
body: <p>(This is intended as a Q/A style question, intended to be a go-to resource for people that ask similar questions. A lot of people seem to stumble on the best way of doing this because they don't know all the options. Many of the answers will be ASP.NET specific, but AJAX and other techniques do have equivalents in other frameworks, such as <a href="http://socket.io/" rel="noreferrer">socket.io</a> and SignalR.)</p>
  
  <p>I have a table of data that I have implemented in ASP.NET. I want to display changes to this underlying data on the page in real time or near real time. How do I go about it?</p>
  
  <p>My Model:</p>
  
  <pre><code>public class BoardGame
      {
      public int Id { get; set;}
      public string Name { get; set;}
      public string Description { get; set;}
      public int Quantity { get; set;}
      public double Price { get; set;}
  
      public BoardGame() { }
      public BoardGame(int id, string name, string description, int quantity, double price)
          {
          Id=id;
          Name=name;
          Description=description;
          Quantity=quantity;
          Price=price;
          }
      }
  </code></pre>
  
  <p>In lieu of an actual database for this example, I'm just going to store the data in the Application variable. I'm going to seed it in my <code>Application_Start</code> function of my Global.asax.cs.</p>
  
  <pre><code>var SeedData = new List&lt;BoardGame&gt;(){
      new BoardGame(1, "Monopoly","Make your opponents go bankrupt!", 76, 15),
      new BoardGame(2, "Life", "Win at the game of life.", 55, 13),
      new BoardGame(3, "Candyland", "Make it through gumdrop forrest.", 97, 11)
      };
  Application["BoardGameDatabase"] = SeedData;
  </code></pre>
  
  <p>If I were using Web Forms, I'd display the data with a repeater.</p>
  
  <pre><code>&lt;h1&gt;Board Games&lt;/h1&gt;
          &lt;asp:Repeater runat="server" ID="BoardGameRepeater" ItemType="RealTimeDemo.Models.BoardGame"&gt;
              &lt;HeaderTemplate&gt;
                  &lt;table border="1"&gt;
                      &lt;tr&gt;
                          &lt;th&gt;Id&lt;/th&gt;
                          &lt;th&gt;Name&lt;/th&gt;
                          &lt;th&gt;Description&lt;/th&gt;
                          &lt;th&gt;Quantity&lt;/th&gt;
                          &lt;th&gt;Price&lt;/th&gt;
                      &lt;/tr&gt;
              &lt;/HeaderTemplate&gt;
              &lt;ItemTemplate&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;%#: Item.Id %&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;%#: Item.Name %&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;%#: Item.Description %&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;%#: Item.Quantity %&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;%#: Item.Price %&gt;&lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/ItemTemplate&gt;
              &lt;FooterTemplate&gt;&lt;/table&gt;&lt;/FooterTemplate&gt;
          &lt;/asp:Repeater&gt;
  </code></pre>
  
  <p>And load that data in the code behind:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
      {
      BoardGameRepeater.DataSource = Application["BoardGameDatabase"];
      BoardGameRepeater.DataBind();
      }
  </code></pre>
  
  <p>If this were MVC using Razor, it's just a simple foreach over the model:</p>
  
  <pre><code>@model IEnumerable&lt;RealTimeDemo.Models.BoardGame&gt;
  &lt;h1&gt;Board Games&lt;/h1&gt;
  &lt;table border="1"&gt;
      &lt;tr&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Id)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Name)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Description)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Quantity)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Price)
          &lt;/th&gt;
      &lt;/tr&gt; 
  @foreach (var item in Model) {
      &lt;tr&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.Id)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.Name)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.Description)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.Quantity)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.Price)
          &lt;/td&gt;
      &lt;/tr&gt;
  } 
  &lt;/table&gt;
  </code></pre>
  
  <p>Let's use Web Forms to have a little page for adding data so we can then watch the data update in real time. I recommend you create two browser windows so you can see the form and table at the same time.</p>
  
  <pre><code>&lt;h1&gt;Create&lt;/h1&gt;
  &lt;asp:Label runat="server" ID="Status_Lbl" /&gt;&lt;br /&gt;
  Id: &lt;asp:TextBox runat="server" ID="Id_Tb" /&gt;&lt;br /&gt;
  Name: &lt;asp:TextBox runat="server" ID="Name_Tb" /&gt;&lt;br /&gt;
  Description: &lt;asp:TextBox runat="server" ID="Description_Tb" /&gt;&lt;br /&gt;
  Quantity: &lt;asp:TextBox runat="server" ID="Quantity_Tb" /&gt;&lt;br /&gt;
  Price: &lt;asp:TextBox runat="server" ID="Price_Tb" /&gt;&lt;br /&gt;
  &lt;asp:Button runat="server" ID="SubmitBtn" OnClick="SubmitBtn_Click" Text="Submit" /&gt;
  </code></pre>
  
  <p>And the code behind:</p>
  
  <pre><code>protected void SubmitBtn_Click(object sender, EventArgs e)
      {
      var game = new BoardGame();
      game.Id = Int32.Parse(Id_Tb.Text);
      game.Name = Name_Tb.Text;
      game.Description = Description_Tb.Text;
      game.Quantity = Int32.Parse(Quantity_Tb.Text);
      game.Price = Int32.Parse(Price_Tb.Text);
      var db = (List&lt;BoardGame&gt;)Application["BoardGameDatabase"];
      db.Add(game);
      Application["BoardGameDatabase"] = db;
      //only for SignalR
      /*var context = GlobalHost.ConnectionManager.GetHubContext&lt;GameHub&gt;();
      context.Clients.All.addGame(game); */           
      }
  </code></pre>