无法在ASP.NET中投射System.Web.UI.WebControls.GridView类型的对象 [英] Unable to cast object of type System.Web.UI.WebControls.GridView in ASP.NET

查看:121
本文介绍了无法在ASP.NET中投射System.Web.UI.WebControls.GridView类型的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

然而,当我按下这些按钮(编辑/删除)并引发异常时。
无法投射类型为'System.Web.UI.WebControls.GridView'的对象以键入'System.Web.UI.WebControls.Button'。 p>

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;

这里的问题是这一行与编辑或删除方法无关。它与另一列中的asp按钮有关,因此我迷路了。我该如何解决这个问题?什么导致OnRowDeleting和OnRowEditing冲突与showResponses方法?

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?

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

任何帮助都不胜感激。

推荐答案

对我来说这似乎很合理。这里:

It seems reasonably clear to me. Here:

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

您将 RowCommand 事件至 showResponses 。在这里,在 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");            
}

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

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

这篇关于无法在ASP.NET中投射System.Web.UI.WebControls.GridView类型的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆