title: Async behavior on WebForms gridview
body: <p>While porting a legacy application to async\await pattern we've came across some weird behavior we can't track. We set the page async directive to true, and add event handler to RowDataBound. If the code in the EventHandler contains an awaited call, it is not awaited before moving on to handle the next row.</p>
  
  <p>Here is a repro to make it easier to understand:</p>
  
  <pre><code>public partial class WorkingCorrectlyWebForm: System.Web.UI.Page
  {
      private int _index;
      protected void Page_Load(object sender, EventArgs e)
      {
          var grid = new GridView();
          grid.RowDataBound += GridOnRowDataBound;
          grid.DataSource = new[]
          {
              new {Name = "Person1", Age = 23},
              new {Name = "Person2", Age = 33},
              new {Name = "Person3", Age = 15}
          };
  
          grid.DataBind();
      }
  
      private async void GridOnRowDataBound(object sender, GridViewRowEventArgs gridViewRowEventArgs)
      {
          if (gridViewRowEventArgs.Row.RowType != DataControlRowType.DataRow)
              return;
  
          var localIndex = ++_index;
  
          HttpContext.Current.Response.Write($"starting #{localIndex} &lt;br /&gt;");
  
          await Task.Delay(1000); //HERE IS THE PROBLEMATIC LINE
  
          //removing the remark from the following line will make code behave "synchronously" as expected.
          // Task.Delay(1000).Wait(); 
  
          HttpContext.Current.Response.Write($"exiting #{localIndex} &lt;br /&gt;");
      }
  }
  </code></pre>
  
  <p>This code will generate output that is similiar to this (instead of ordered "starting\exiting" each item):</p>
  
  <p>starting #1 </p>
  
  <p>starting #2 </p>
  
  <p>starting #3 </p>
  
  <p>exiting #3 </p>
  
  <p>exiting #1 </p>
  
  <p>exiting #2 </p>
  
  <p>Why is this happeninig? why am i not seeing an ordered "starting" and "exiting" messages.</p>