title: Unable to cast object of type System.Web.UI.WebControls.GridView in ASP.NET
body: <p>I wrote a method that deletes rows from my an asp.net Gridview when the delete button is clicked and another method for when the edit button is clicked.
  Both Edit and Delete buttons are part of the built in gridview controls.</p>
  
  <p>However when I press these buttons (edit/delete)and exception is thrown.
  <strong>Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.Button'.</strong> which is pointing at the line</p>
  
  <pre><code>Button btn = (Button)sender;
  </code></pre>
  
  <p>The problem here is that this line is not related to either of the edit or delete methods. It is related to the asp button in another column, and for that reason I am lost. How can I resolve this issue? What is causing both the OnRowDeleting and OnRowEditing conflict with the showResponses method?</p>
  
  <p>Here is the aspx</p>
  
  <pre><code>&lt;asp:GridView runat="server" ID="gvShowQuestionnaires" HeaderStyle-CssClass="table_header" CssClass="view" AlternatingRowStyle-CssClass="alt" AlternatingRowStyle-BackColor="#f3f4f8" AutoGenerateColumns="False" 
                  DataKeyNames='QuestionnaireID' OnRowDeleting="gvShowQuestionnaires_RowDeleting" OnRowEditing="gvShowQuestionnaires_RowEdit" FooterStyle-CssClass="view_table_footer" OnRowCommand="showResponses"&gt; 
      &lt;Columns&gt;
          &lt;asp:BoundField DataField="QuestionnaireID" HeaderText="ID" HeaderStyle-Width="80px" ItemStyle-CssClass="bo"&gt;&lt;/asp:BoundField&gt;
          &lt;asp:BoundField DataField="QuestionnaireName" HeaderText="Questionnaire Name" /&gt;           
          &lt;asp:TemplateField HeaderText="Results" HeaderStyle-Width="150px"&gt;
              &lt;ItemTemplate&gt;
                 &lt;asp:Button runat="server" ID="button1" CommandArgument='&lt;%# Eval("QuestionnaireID") %&gt;' OnClick="showResponses" text="Results"/&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:CommandField HeaderText="Options" ShowDeleteButton="True" ShowEditButton="true" EditText="Edit"&gt;&lt;/asp:CommandField&gt;
      &lt;/Columns&gt; 
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>And here is the code behind:</p>
  
  <pre><code>protected void gvShowQuestionnaires_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
      int questionnaireID = (int)gvShowQuestionnaires.DataKeys[Convert.ToInt32(e.RowIndex)].Value;
      GetData.DeleteQuestionnaire(questionnaireID);
      gvShowQuestionnaires.DataSource = DT;
      gvShowQuestionnaires.DataBind();
  }
  
  protected void gvShowQuestionnaires_RowEdit(object sender, GridViewEditEventArgs e)
  {
     string id = gvShowQuestionnaires.Rows[e.NewEditIndex].Cells[0].Text;
     Session["qID"] = id;
     Response.Redirect("~/members/edit_questionnaire.aspx");
  }
  
  protected void showResponses(object sender, EventArgs e)
  {
      Button btn = (Button)sender;
      string id = btn.CommandArgument.ToString();
      Session["qID"] = id;
      Response.Redirect("~/members/questionnaire_responses.aspx");            
  }
  </code></pre>
  
  <p>Any help would much appreciated.</p>