title: button and itemcommand event sequence
body: <p><strong>Background information</strong>:</p>
  
  <ol>
  <li>I have a SQL connected datalist, one of the columns is called <code>work_order</code></li>
  <li>In the datalist I have inserted a button <code>btn_Start</code>. The button is populated at the end of each set</li>
  <li>The goal of the <code>btn_Start</code> is to do a database insert, the insert needs to includes the <code>work_order</code> value from the set of data the button is clicked in (so the insert can be tied to the work_order value.)  </li>
  </ol>
  
  <p><code>btn_Start</code> code:</p>
  
  <pre><code>protected void btn_Start(object sender, EventArgs e)
  {
      using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["nothanks"].ConnectionString))
      {
          String query = "INSERT INTO [TimeTest] ([Starttime], [Work_Order]) VALUES (@Starttime, @Work_Order)";
  
          using (SqlCommand CCC = new SqlCommand(query, connection))
          {
              connection.Open();
              CCC.CommandType = CommandType.Text;
  
              CCC.Parameters.Add("@Starttime", SqlDbType.DateTime).Value = DateTime.Now;
              CCC.Parameters.Add("@Work_Order", SqlDbType.Int).Value = lb_User1.Text.ToString();
  
              CCC.ExecuteNonQuery();
          }
      }
  }
  </code></pre>
  
  <ol start="4">
  <li>To grab text of the <code>work_order</code> column, I'm using the <code>itemcommand</code> event to propagate a label (<code>lb_User1</code>). </li>
  </ol>
  
  <p>DataList1_ItemCommand` code:</p>
  
  <pre><code>protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
  {
          DataList2.SelectedIndex = e.Item.ItemIndex;
          lb_User1.Text = (DataList2.SelectedItem.FindControl("Work_OrderLabel4") as Label).Text;
  }
  </code></pre>
  
  <ol start="5">
  <li>This works well, each time <code>btn_Start</code> is pushed, <code>lb_User1</code> is updated with the right information. </li>
  </ol>
  
  <p><strong>The issue</strong>: when <code>btn_Start</code> is clicked, both <code>btn_Start</code> and <code>DataList1_ItemCommand</code> fire. But <code>DataList1_ItemCommand</code> fires <strong>after</strong> <code>btn_Start</code>. Which means <code>lb_User1</code> isn't updated with the right info yet, and as such the insert fails to work as needed. </p>
  
  <p>NOTES:</p>
  
  <ol>
  <li><p>the <code>lb_User1</code> isn't needed, I planned to go direct to the SQL insert. <code>lb_User1</code> was used for code testing (so I can see what's going on)</p></li>
  <li><p>My <strong>objective</strong> is to do the SQL insert with the grabbed data from datalist (<code>Work_OrderLabel4</code>). If I can accomplish this objective a better way that would also solve the issue.</p></li>
  <li><p><code>btn_Start</code> isn't going to be the only button in the datalist. One possible solution is go way from having two events and only doing things under the <code>itemcommand</code> event, but how do you separate out which button fires, without involving their respective events. </p></li>
  </ol>
  
  <p><strong>Objective</strong>: I'm trying to get each embedded start button to grab its corresponding <code>work_order</code> value for use in a SQL insert. The above is trying to accomplish this task, I'm almost there but I'm having the issue stated above. I'm open for other ways to accomplish this task (see picture of clarification) </p>
  
  <p><a href="https://i.stack.imgur.com/Zq2G7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Zq2G7.png" alt="enter image description here"></a></p>
  
  <p><strong>Additional Information:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/CxVLm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CxVLm.png" alt="enter image description here"></a></p>
  
  <pre><code> protected void DL_Main_ItemCommand(object source, DataListCommandEventArgs e)
  {
      using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["test"].ConnectionString))
      {
          String query = "INSERT INTO [TimeTest] ([Starttime], [Work_Order]) VALUES (@Starttime, @Work_Order)";
  
          using (SqlCommand CCC = new SqlCommand(query, connection))
          {
              connection.Open();
              CCC.CommandType = CommandType.Text;
              CCC.Parameters.Add("@Starttime", SqlDbType.DateTime).Value = DateTime.Now;
           // All you need is the value of Work_OrderLabel4 of the selected item so just do it like this. 
              CCC.Parameters.Add("@Work_Order", SqlDbType.Int).Value = (DL_Main.SelectedItem.FindControl("Work_OrderLabel") as Label).Text;
  
  
              CCC.ExecuteNonQuery();
          }
      }
  }
  </code></pre>
  
  <p>After Running
  <a href="https://i.stack.imgur.com/eMfod.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eMfod.png" alt="After Running"></a></p>