Unable to cast object of type System.Web.UI.WebControls.GridView in ASP.NET

37,955

It seems reasonably clear to me. Here:

<asp:GridView runat="server" ... OnRowCommand="showResponses"> 

you bind the RowCommand event to showResponses. And here, in showResponses, you assume that the sender is a button:

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");            
}

The sender isn't a button - it's the grid view. If you want the command argument, you should use GridViewCommandEventArgs.CommandArgument.

protected void showResponses(object sender, GridViewCommandEventArgs e)
{
    Session["qID"] = e.CommandArgument;
    Response.Redirect("~/members/questionnaire_responses.aspx");            
}
Share:
37,955
HGomez
Author by

HGomez

Hello Sir.

Updated on June 13, 2020

Comments

  • HGomez
    HGomez almost 4 years

    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.

    However when I press these buttons (edit/delete)and exception is thrown. Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.Button'. which is pointing at the line

    Button btn = (Button)sender;
    

    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?

    Here is the aspx

    <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"> 
        <Columns>
            <asp:BoundField DataField="QuestionnaireID" HeaderText="ID" HeaderStyle-Width="80px" ItemStyle-CssClass="bo"></asp:BoundField>
            <asp:BoundField DataField="QuestionnaireName" HeaderText="Questionnaire Name" />           
            <asp:TemplateField HeaderText="Results" HeaderStyle-Width="150px">
                <ItemTemplate>
                   <asp:Button runat="server" ID="button1" CommandArgument='<%# Eval("QuestionnaireID") %>' OnClick="showResponses" text="Results"/>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:CommandField HeaderText="Options" ShowDeleteButton="True" ShowEditButton="true" EditText="Edit"></asp:CommandField>
        </Columns> 
    </asp:GridView>
    

    And here is the code behind:

    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");            
    }
    

    Any help would much appreciated.

  • HGomez
    HGomez over 12 years
    Thanks. I am getting an error No overload for 'showResponses' matches delegate 'System.EventHandler' on the line <asp:Button runat="server" ID="button1" CommandArgument='<%# Eval("QuestionnaireID") %>' OnClick="showResponses" text="Results"/> How can I solve this?
  • Jon Skeet
    Jon Skeet over 12 years
    @Rupert: Use a different handler for that event, basically. You may well want to make both handlers call into the same single method after extracting the CommandArgument appropriately.
  • HGomez
    HGomez over 12 years
    @JonSkeet Which event handler should be used? It appears that the onRowCommand is conflicting with the OnRowDeleting handler. How would I extract the relevant CommandArgument? I have limited experience with the Gridview control so I would not know which event handler to use.
  • Jon Skeet
    Jon Skeet over 12 years
    @Rupert: I don't know the details of when which event is used - but you already know how to get the command argument from a button click - because you had that code before. You just can't use the same code for both the button click and the RowCommand event.
  • HGomez
    HGomez over 12 years
    Finally figured it out. Thanks =]