Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7070779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:36:27+00:00 2026-05-28T05:36:27+00:00

I wrote a method that deletes rows from my an asp.net Gridview when the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:36:28+00:00Added an answer on May 28, 2026 at 5:36 am

    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");            
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a method that extracts fields from an object like this: private static
I have and old(ish) C# method I wrote that takes a number and converts
I wrote a windows service in C# that does some functionality (a public method)
I am trying to write a method that gets files from a folder, orders
I would like to write a method that deletes my application's Core Data store,
I have a problem with deleting my objects. I wrote a delete method which
I'm trying to write a code that deletes all rows between the 3rd and
How can I write a method that deletes the last value of a list
I wrote a UnitOfWork implementation that does not expose a public Commit() method. Instead
I have a method that get text from a JTextArea, create a file and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.