title: Add OnClick event for asp.net grid view
body: <p>I have added new event i.e Gridview_Onclick event dynamically in RowDatabound event. But when I try to handle that event in the server side code, I can code for that event. This is the code for my grid view.</p>
  
  <pre><code>&lt;%@ Control Language="C#" AutoEventWireup="true" CodeFile="PopUp.ascx.cs" Inherits="PopUp" %&gt;
  &lt;%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %&gt;
  
  &lt;table class="style1"&gt;
  &lt;tr&gt;
      &lt;td&gt;
          &lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
              CellPadding="4" ForeColor="#333333" GridLines="None" 
              onrowdatabound="GridView1_RowDataBound"&gt;
              &lt;AlternatingRowStyle BackColor="White" ForeColor="#284775" /&gt;
              &lt;Columns&gt;
                  &lt;asp:BoundField DataField="DeptId" HeaderText="ID"/&gt;
                  &lt;asp:BoundField DataField="Name" HeaderText="Name"/&gt;
                  &lt;asp:BoundField DataField="HeadName" HeaderText="HOD"/&gt;
                  &lt;asp:BoundField DataField="HeadEmail" HeaderText="Email ID"/&gt;
              &lt;/Columns&gt;
              &lt;EditRowStyle BackColor="#999999" /&gt;
              &lt;FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /&gt;
              &lt;HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /&gt;
              &lt;PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /&gt;
              &lt;RowStyle BackColor="#F7F6F3" ForeColor="#333333" /&gt;
              &lt;SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /&gt;
              &lt;SortedAscendingCellStyle BackColor="#E9E7E2" /&gt;
              &lt;SortedAscendingHeaderStyle BackColor="#506C8C" /&gt;
              &lt;SortedDescendingCellStyle BackColor="#FFFDF8" /&gt;
              &lt;SortedDescendingHeaderStyle BackColor="#6F8DAE" /&gt;
          &lt;/asp:GridView&gt;
          &lt;asp:Button ID="btnTest" runat="server" style="display:none" /&gt;
      &lt;/td&gt;
  &lt;/tr&gt; 
  &lt;/table&gt;
  </code></pre>
  
  <p>This is the code for my ascs.cs page</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      oCommand.CommandText = "select * from Department";
      oCommand.Connection = oConnection;
      oAdapter.SelectCommand = oCommand;
      DataTable dt = new DataTable();
      oAdapter.Fill(dt);
      GridView1.DataSource = dt;
      GridView1.DataBind();
  }
  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
      e.Row.Attributes["OnClick"] = Page.ClientScript.GetPostBackClientHyperlink(e.Row, string.Empty);
  }
  protected void GridView1_OnClick(object sender, GridViewRowEventArgs e)
  {
      _ID = "10";
      _Name = "abc";
      _HOD = "abc";
      _Email = "abc";
  }
  </code></pre>
  
  <p>The grid view opens in modelpopupextender so when I click anywhere in grid, its getting closed which is not the case of I remove the event that is added dynamically. In short the event is added but can not be handled in code.
  Please guide me how can I catch this event in coding??</p>