title: Button onClick does not fire in GridView inside of TemplateField
body: <p>I'm trying to write a code that would update only one record for the data.</p>
  
  <p>I do not need to be able to update every field on the row, only one particular field.</p>
  
  <p>I have page that defines a GridView as following:</p>
  
  <pre><code>&lt;asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent"&gt;
  &lt;UpdatePanel&gt; 
      &lt;ContentTemplate&gt;   
   &lt;fieldset style="width: 1%"&gt;
          &lt;legend&gt;Search Customer&lt;/legend&gt;
          &lt;table&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                  &lt;asp:DropDownList id="ddlSearchOption"
                      AutoPostBack="True"
                      OnSelectedIndexChanged="ddlSearchOption_SelectedIndexChanged"
                      runat="server"&gt;
                      &lt;asp:ListItem Selected="True" Value="SearchBy"&gt; Search By &lt;/asp:ListItem&gt;
                      &lt;asp:ListItem Value="CustID"&gt; Customer ID &lt;/asp:ListItem&gt;
                      &lt;asp:ListItem Value="CustFirst"&gt; First Name &lt;/asp:ListItem&gt;
                      &lt;asp:ListItem Value="CustLast"&gt; Last Name &lt;/asp:ListItem&gt;
                      &lt;asp:ListItem Value="CustCity"&gt; City &lt;/asp:ListItem&gt;
                  &lt;/asp:DropDownList&gt;
                  &lt;/td&gt;
        &lt;asp:Panel id="pnlCustSearch" runat="server" &gt;
                      &lt;td&gt;
               &lt;asp:Label id="lblEntry" runat="server" Text="" width="120px"&gt;&lt;/asp:Label&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:TextBox id="txtSearchOptions" runat="server" width="50px"&gt;Hello&lt;/asp:TextBox&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:Button id="btnFind" Text="Search" runat="server" onClick="btnFind_Click"&gt;&lt;/asp:Button&gt;
                      &lt;/td&gt;
      &lt;/asp:Panel&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
      &lt;/fieldset&gt;
      &lt;div id ="msg"&gt;
      &lt;asp:Label id="lblMsg" runat="server" Text=""&gt;&lt;/asp:Label&gt;
      &lt;/div&gt;
      &lt;div id="gridShow"&gt;
  
    &lt;asp:GridView ID="GridView1" runat="server" GridLines="None" AutoGenerateColumns="false"
    AlternatingRowStyle-BackColor="#EEEEEE" EditRowStyle-BorderColor="Red"&gt;           
    &lt;Columns&gt;           
       &lt;asp:TemplateField Visible="true" HeaderText="Customer ID"&gt;
          &lt;ItemTemplate&gt;
            &lt;asp:Label runat="server" ID="lblCustID" Text='&lt;%#Eval("CustID")%&gt;' /&gt;
          &lt;/ItemTemplate&gt;
       &lt;/asp:TemplateField&gt;
  
       &lt;asp:TemplateField HeaderText="First Name"&gt;
  
          &lt;ItemTemplate&gt;
           &lt;asp:Label runat="server" ID="lblFirstName" Text='&lt;%#Eval("CustFirstName") %&gt;' /&gt;
          &lt;/ItemTemplate&gt;
       &lt;/asp:TemplateField&gt;
       &lt;asp:TemplateField HeaderText="Last Name"&gt;
          &lt;ItemTemplate&gt;
            &lt;asp:Label runat="server" ID="lblLastName" Text='&lt;%#Eval("CustLastName") %&gt;' /&gt;
          &lt;/ItemTemplate&gt;
       &lt;/asp:TemplateField&gt;
  
       &lt;asp:TemplateField HeaderText="City"&gt;
         &lt;ItemTemplate&gt;
           &lt;asp:Label runat="server" ID="lblCity" Text='&lt;%#Eval("CustCity") %&gt;' /&gt;
         &lt;/ItemTemplate&gt;
       &lt;/asp:TemplateField&gt;
       &lt;asp:TemplateField HeaderText="Email"&gt;  
        &lt;ItemTemplate&gt;
           &lt;asp:TextBox runat="server" ID="txtEmail" Text='&lt;%#Eval("CustEmail") %&gt;' /&gt;
            &lt;asp:Button runat="server" ID="btnUpdate" Text="Update" onClick="GridView1_btnUpdate_Click"/&gt;
    &lt;asp:RequiredFieldValidator runat="server" ID="rfdCountry" ControlToValidate="txtEmail" 
               ValidationGroup="var1" ErrorMessage="*" /&gt;
         &lt;/ItemTemplate&gt;
       &lt;/asp:TemplateField&gt;
  
   &lt;/Columns&gt;
   &lt;/asp:GridView&gt;
  
  &lt;/div&gt;
  &lt;/ContentTemplate&gt;
  &lt;/UpdatePanel&gt; 
  
   &lt;/asp:Content&gt;
  </code></pre>
  
  <p>Then in codebehind I write the following:</p>
  
  <pre><code>    protected void GridView1_btnUpdate_Click(Object sender, EventArgs e)
      {
         //Code goes here
      }
  </code></pre>
  
  <p>When clicking button to update email nothing happens.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>I tried to use one of suggestions here:</p>
  
  <pre><code>        if (!IsPostBack)
          {
              GridView1.DataSource = App_Data.DataHandler.GetData("fname", "de");
              GridView1.DataBind();
              GridView1.Visible = true;
          }
  </code></pre>
  
  <p>The data is displayed but the click event does not work anyway</p>
  
  <p>I added the following to my GridView definition:</p>
  
  <pre><code>onrowcommand="GridView1_RowCommand"
  </code></pre>
  
  <p>and added the code suggested:</p>
  
  <pre><code>             &lt;asp:Button runat="server" ID="btnUpdate" Text="Update" CommandName="UpdateEmail" 
               CommandArgument="&lt;%# ((GridViewRow) Container).RowIndex %&gt;"/&gt;
  </code></pre>
  
  <p>In my <code>.cs</code> file I added the following:</p>
  
  <pre><code>        protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
      {
          if (e.CommandName == "UpdateEmail")
          {
              //Get the index from the argument
              int index = Convert.ToInt32(e.CommandArgument);
              //Get the row
              GridViewRow row = GridView1.Rows[index];
  
              //Do whatever you need with your row here!
          }
      }
  </code></pre>
  
  <p>Still, click event does not fire.</p>
  
  <p>Ok, here is my code behind <code>.cs</code> file:</p>
  
  <pre><code>public partial class index : System.Web.UI.Page
  {
      protected override object LoadPageStateFromPersistenceMedium()
      {
          return Session["__VIEWSTATE"];
      }
      protected override void SavePageStateToPersistenceMedium(object viewState)
      {
          Session["VIEWSTATE"] = viewState;
      }
  
      protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
              Reset();
          else
              lblMsg.Text = "";
  
      }
  
      protected void Reset()
      {
          pnlCustSearch.Visible = false;
          GridView1.Visible = false;
          lblMsg.Text = "";
      }
  
      protected void ddlSearchOption_SelectedIndexChanged(object sender, EventArgs e)
      {
  
              Reset();
              String ddlSelected = ddlSearchOption.SelectedValue;
              if (ddlSelected != "")
              {
                  ViewState["ddlSelected"] = ddlSelected;
                  pnlCustSearch.Visible = true;
                  SelectLabel(ddlSelected);
              }
      }
  
      protected void SelectLabel(String choice)
      {
          switch (choice)
          {
              case "CustID":
                  lblEntry.Text = "Enter Customer ID";
                  break;
              case "CustFirst":
                  lblEntry.Text = "Enter First Name";
                  break;
              case "CustLast":
                  lblEntry.Text = "Enter Last Name";
                  break;
              case "CustCity":
                  lblEntry.Text = "Enter City";
                  break;
              default: 
                  lblEntry.Text = "Enter Customer ID";
                  break;
          }
      }
  
      protected void btnFind_Click(object sender, EventArgs e)
      {
          GridView1.Visible = true;
          String option = (String)ViewState["ddlSelected"];
          String input = txtSearchOptions.Text.Trim();
  
          GridView1.DataSource = DataHandler.GetData(option,input);
          GridView1.DataBind();
          GridView1.Visible = true;
  
      }
  
      protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
      {
          if (e.CommandName == "UpdateEmail")
          {
              //Get the index from the argument
              int index = Convert.ToInt32(e.CommandArgument);
              //Get the row
              GridViewRow row = GridView1.Rows[index];
  
              //Do whatever you need with your row here!
          }
      }
  
      protected void btnUpdate_Click(Object sender, EventArgs e)
      {
          String txt = null;
          txt = "here";
      }
  }
  </code></pre>