C# 无法在 ASP.NET 中转换 System.Web.UI.WebControls.GridView 类型的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8919321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:10:25  来源:igfitidea点击:

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

c#asp.netexceptiongridview

提问by HGomez

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.

我编写了一个方法,当单击删除按钮时从我的 asp.net Gridview 中删除行,并在单击编辑按钮时从另一个方法中删除行。编辑和删除按钮都是内置 gridview 控件的一部分。

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

但是,当我按下这些按钮(编辑/删除)并抛出异常时。 无法将“System.Web.UI.WebControls.GridView”类型的对象转换为“System.Web.UI.WebControls.Button”类型。这是指向线

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?

这里的问题是这一行与 edit 或 delete 方法都不相关。它与另一列中的 asp 按钮有关,因此我迷路了。我该如何解决这个问题?是什么导致 OnRowDeleting 和 OnRowEditing 与 showResponses 方法发生冲突?

Here is the aspx

这是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.

任何帮助将不胜感激。

采纳答案by Jon Skeet

It seems reasonably clear to me. Here:

这对我来说似乎很清楚。这里:

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

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

您将RowCommand事件绑定到showResponses. 在这里,在 中showResponses,您假设发件人是一个按钮:

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'ta button - it's the grid view. If you want the command argument, you should use GridViewCommandEventArgs.CommandArgument.

发件人不是按钮 - 它是网格视图。如果你想要命令参数,你应该使用GridViewCommandEventArgs.CommandArgument.

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

回答by Jagd

Add a CommandName attribute to your button1 in the GridView.

将 CommandName 属性添加到 GridView 中的 button1。

<asp:Button runat="server" ID="button1" CommandName="Button1" CommandArgument='<%# Eval("QuestionnaireID") %>' OnClick="showResponses" text="Results"/>

Then in showResponses do this...

然后在 showResponses 中执行此操作...

protected void showResponses(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Button1") {
         Session["qID"] = e.CommandArgument;
         Response.Redirect("~/members/questionnaire_responses.aspx");
    }
}

回答by boateng

var btnSender = (Button)e.CommandSource;