title: Execute Javascript function from GridView ItemTemplate button click ASP.NET Web Forms
body: <p>I have a DataGridView that has two ItemTemplate button columns that are dynamically generated. I have code-behind for the both of the buttons that fire when they are clicked, but I also need a javascript function to run when the btnInfo button is clicked</p>
  
  <p>markup:</p>
  
  <pre><code> &lt;asp:GridView ID="gridResutls" runat="server" OnRowCommand="gridResutls_RowCommand" AutoGenerateColumns="False"&gt;
          &lt;Columns&gt;
              &lt;asp:BoundField DataField="strName" HeaderText="Name"/&gt;
              &lt;asp:BoundField DataField="strBrand" HeaderText="Brand"/&gt;
              &lt;asp:BoundField DataField="strServing" HeaderText="Serving"/&gt;
                      &lt;asp:TemplateField ShowHeader="False"&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Button ID="btnInfo" runat="server" CausesValidation="false" CommandName="MoreInfo"
                              Text="More Info" CommandArgument='&lt;%# Eval("strID") %&gt;'/&gt;
                          &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                      &lt;asp:TemplateField ShowHeader="False"&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Button ID="btnAdd" runat="server" CausesValidation="false" CommandName="AddItem"
                              Text="Add To Log" CommandArgument='&lt;%# Eval("strID") %&gt;' /&gt;
                          &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
          &lt;/Columns&gt;
      &lt;/asp:GridView&gt;'
  </code></pre>
  
  <p>I have a javascript function called <code>populateLabel()</code> that I need to fire off when btnInfo is clicked.</p>
  
  <p>Any ideas? (I know similar questions have been asked and I looked throughout the posts and tried a few things but nothing seems to work)</p>
  
  <p>EDIT: Here is what the code-behind for the ItemTemplate buttons looks like</p>
  
  <pre><code>protected void gridResutls_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
      {
  
          // Kick out if neither of the two dynamically created buttons have been clicked
          if (e.CommandName != "MoreInfo" &amp;&amp; e.CommandName != "AddItem")
          {
              return;
          }
  
          // If the "More Info" buton has been clicked
          if (e.CommandName == "MoreInfo")
          {
               // Some code
          }
  
          // If the "Add To Log" button has been clicked
          if (e.CommandName == "AddItem")
          {
              // Some code
          }
      }
  </code></pre>