title: ASP.NET 4.0 Webforms - Update 3 panels asynchronously
body: <p>Maybee I'm thinking of this the wrong way, I know the page renders and is sent to the client(webbrowser). But I really need to get to understand this.</p>
  
  <p><strong>Question</strong>
  How do I update my three updatepanels asynchronously? To simulate this I have created a for loop which adds a second each time, after correct number is found and event is fired.
  So what I want to do is to be able to see in the webbrowser, when these updates are done.
  The time span would be like this:</p>
  
  <p><strong>TIME</strong><br>
  0,0235(just a number toshow it really happens quick)<br>
  <strong>What happens on the page?</strong>
  Almost instantly first label get's updated with number 7
  <strong>TIME</strong><br>
  0,0235 + one second = 1,0235 
  <strong>What happens on the page?</strong>
  Almost instantly second label get's updated with number 8
  <strong>TIME</strong><br>
  0,0235 + one second = 2,0235 
  <strong>What happens on the page?</strong>
  Almost instantly third label get's updated with number 9</p>
  
  <p>I ahve visted pages that first show you some text and after a couple of seconds they show you a diagram, when that request has finished loading? So there must be a way rigth?</p>
  
  <p>Heres my code, I'm not posting my CounterEventArgs class it works and doesn't affect the question.</p>
  
  <p>Here's my index.aspx - codebehind
  public partial class _Default : System.Web.UI.Page
  {
      private testClass _testet;</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      ScriptManager1.RegisterAsyncPostBackControl(Button1);  
  }
  void _testet_CounterFoundNumber(object sender, CounterEventArgs e)
  {
      switch (e.labelnumber)
      {
          case 1:
              Label1.Text = e.positionen.ToString();            
              break;
          case 2:
              Label2.Text = e.positionen.ToString();        
              break;
          case 3:
              Label3.Text = e.positionen.ToString();        
              break;
      } 
  }
  protected void Button1_Click(object sender, EventArgs e)
  {
      testClass testet = new testClass();
      _testet = testet;
      _testet.CounterFoundNumber += new testClass.CounterEventArgsHandler(_testet_CounterFoundNumber);
      _testet.count();
  }
  }
  </code></pre>
  
  <p>And the Source</p>
  
  <pre><code>  &lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Async="true"%&gt;
  
    &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
   &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
   &lt;head runat="server"&gt;
  &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;form id="form1" runat="server"&gt;
    &lt;div&gt;  
      &lt;asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /&gt;
  &lt;/div&gt;
  &lt;asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"&gt;
  &lt;/asp:ScriptManager&gt;
  &lt;asp:UpdatePanel ID="updatepanel1" runat="server" UpdateMode="Always"&gt;
  &lt;ContentTemplate&gt;
  &lt;asp:Label ID="Label1" runat="server" Text="Label"&gt;&lt;/asp:Label&gt;
  &lt;/form&gt;
  &lt;/ContentTemplate&gt;
  &lt;/asp:UpdatePanel&gt;
  
   &lt;asp:UpdatePanel ID="updatepanel2" runat="server" UpdateMode="Always"&gt;
  &lt;ContentTemplate&gt;
    &lt;asp:Label ID="Label2" runat="server" Text="Label"&gt;&lt;/asp:Label&gt;
  &lt;/form&gt;
  &lt;/ContentTemplate&gt;
  &lt;/asp:UpdatePanel&gt;
   &lt;asp:UpdatePanel ID="updatepanel3" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional"&gt;
  &lt;ContentTemplate&gt;
  </code></pre>
  
  <p>
      
      
      
      
  
  </p>
  
  <p>And lastly my testclass</p>
  
  <pre><code>public class testClass
  {
  public event CounterEventArgsHandler CounterFoundNumber; 
  public delegate void CounterEventArgsHandler(object sender, CounterEventArgs e);
  public int y = 1;
  
      protected virtual void OnCounterFoundNumber(CounterEventArgs e)
      {
          if (CounterFoundNumber != null)
          CounterFoundNumber.BeginInvoke(this, e, new AsyncCallback(this.ResultFoundNumber), null);
      }
  
      public void count()
      {          
          for (int i = 0; i &lt; 10; i++)
          {
              if (i &gt;= 7)
              {    
                 OnCounterFoundNumber(new CounterEventArgs(i,y));
                 y++;
                 System.Threading.Thread.Sleep(1000);
              }              
          }
      }
      public void ResultFoundNumber(IAsyncResult ar)
      {
          CounterFoundNumber.EndInvoke(ar);
      }
  }
  </code></pre>