title: Raise DataGrid ItemCommand event from Javascript
body: <p>I am using a DataGrid to display data retrieved from a database and I was wondering if it is possible to raise the grid's ItemCommand event using Javascript.</p>
  
  <p>The snippet below shows roughly what I am trying to do within onclick handler for the DIV removeProductButton. I don't want to use asp:Button or asp:LinkButton as currently the look and feel of the DIV is controlled using CSS and the code should work regardless of the type HTML element used to create the click-able trigger to allow for future look and feel customisations.</p>
  
  <pre><code>&lt;asp:datagrid id="itemGrid" runat="server" cellPadding="0" AutoGenerateColumns="False" GridLines="None"&gt;
      &lt;Columns&gt;
          &lt;asp:TemplateColumn&gt;
              &lt;ItemTemplate&gt;
                  &lt;div class="items"&gt;
                      &lt;div class="product_title"&gt;&lt;%#Eval("ItemID")%&gt;.&amp;nbsp;&lt;%#Eval("ItemDescription")%&gt;&lt;/div&gt;
                      &lt;div class="product_image"&gt;
                          &lt;img id="productImage_&lt;%#Eval("ItemID")%&gt;" src="&lt;%#Eval("ThumbnailURL")%&gt;" /&gt;
                      &lt;/div&gt;
                      &lt;div class="buttons"&gt;
                          &lt;div id="removeProductButton" class="buttonStandard" onclick="Do Something HERE..."&gt;Remove&lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateColumn&gt;
      &lt;/Columns&gt;
  &lt;/asp:datagrid&gt;
  </code></pre>
  
  <p>I have tried using the following code in the grid's ItemCreated event but was not able to get it working</p>
  
  <pre><code>private void itemGrid_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  {
      if (e.Item.ItemType == ListItemType.Item ||
          e.Item.ItemType == ListItemType.AlternatingItem)
      {
          dynamic itemData = e.Item.DataItem;
  
          HtmlGenericControl removeProductButton = (HtmlGenericControl)e.Item.FindControl("removeProductButton");
  
          removeProductButton.Attributes.Add("onclick", ClientScript.GetPostBackEventReference(removeProductButton, ""));
      }
  }
  </code></pre>
  
  <p>Any help would be greatly appreciated.</p>