【问题标题】:Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.LinkButton'. while using pagination无法将“System.Web.UI.WebControls.GridView”类型的对象转换为“System.Web.UI.WebControls.LinkBut​​ton”类型。在使用分页时
【发布时间】:2016-01-17 08:35:36
【问题描述】:

我有一个 Gridview,我在其中放置了两个用于编辑和删除行的链接按钮。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" Width="631px" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" Height="144px" style="text-align: right" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                &nbsp;&nbsp;&nbsp;
                                <asp:LinkButton ID="LinkEdit" runat="server" CommandArgument='<%#Eval("eid") %>'>Edit</asp:LinkButton>
                                &nbsp;|
                                <asp:LinkButton ID="LinkDelete" runat="server" CommandArgument='<%#Eval("eid") %>'>Delete</asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>

但是当我在那个 Gridview 中添加分页并尝试转到第 2、3... 页时,它给了我错误:-

无法将“System.Web.UI.WebControls.GridView”类型的对象转换为“System.Web.UI.WebControls.LinkBut​​ton”类型。

Source Error:
Line 29:     {
Line 30:         string id = e.CommandArgument.ToString();
Line 31:         string cmdText = ((LinkButton)e.CommandSource).Text;
Line 32:         if (cmdText.Equals("Edit"))
Line 33:         {

实际错误显示在第 31 行:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    string id = e.CommandArgument.ToString();
    string cmdText = ((LinkButton)e.CommandSource).Text;
    if (cmdText.Equals("Edit"))
    {
        Response.Redirect("Emp_Edit.aspx?id=" + id);
    }
    else
    {
        Class1.EmpDelete(id);
        Response.Redirect("Emp_Reg.aspx");
    }
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        ShowAll(); 
    }

public void ShowAll()
{
    GridView1.DataSource = Class1.ShowData();
    GridView1.DataBind();
}

【问题讨论】:

  • 请同时提供ShowAll();功能以便更好地审核。
  • ShowAll();是绑定网格的公共方法。
  • e.CommandSource 中断时的值是多少。在 GridView1_RowCommand 上放置一个调试点并重现错误。

标签: c# asp.net gridview paging


【解决方案1】:

错误的原因是 string cmdText = ((LinkButton)e.CommandSource).Text;线 和

(LinkButton)e.CommandSource

部分。 它返回的是一个网格视图的命令源,而您正试图将其转换为链接按钮,所以这是一个无效的转换。

另一件事,gridview_RowCommand 中的 e.CommandArgument 返回 Row 的索引,而不是控件 CommandArgument 的文本或值。

所以,你必须尝试这样的事情

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int index = Convert.ToInt32(e.CommandArgument.ToString());
        LinkButton lb=(LinkButton )GridView1.Rows[index].FindControl("lblCmd");//lblCmd is the Id of Your Link Button
        string id = lb.CommandArgument.ToString();
        string cmdText = lb.Text;
        if (cmdText.Equals("Edit"))
        {
            Response.Redirect("Emp_Edit.aspx?id=" + id);
        }
        else
        {
            Class1.EmpDelete(id);
            Response.Redirect("Emp_Reg.aspx");
        }
    }

但在这种情况下,您的 gridview 编辑按钮必须包含 CommandName 属性,其值为 "Edit"

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument.ToString());
LinkButton lb=(LinkButton )GridView1.Rows[index].FindControl("lblCmd");//lblCmd is the Id of Your Link Button
if(e.CommandName=="Edit")
{
  Response.Redirect("Emp_Edit.aspx?id=" + lb.CommandArgument);
}
else
{
    Class1.EmpDelete(idlb.CommandArgument;
    Response.Redirect("Emp_Reg.aspx");
}
}

【讨论】:

    【解决方案2】:

    像这样尝试并相应地更改所有内容并小心:

    改变视图如下:

    <asp:LinkButton id="LinkEdit" 
               Text="Edit"
               CommandName="Edit" 
               CommandArgument='<%#Eval("eid") %>' 
               runat="server"/>
    
    <asp:LinkButton id="LinkDelete" 
               Text="Delete"
               CommandName="Edit" 
               CommandArgument='<%#Eval("eid") %>' 
               runat="server"/>
    

    将 CodeBehind 更改如下:

    string cmdText = e.CommandName; // Line 31
    

    或者也改变你的方法:

    <asp:LinkButton id="LinkEdit" 
                   Text="Edit"
                   CommandName="Edit" 
                   CommandArgument='<%#Eval("eid") %>'
                   OnCommand="LinkButton_Command" 
                   runat="server"/>
    

    void LinkButton_Command(Object sender, CommandEventArgs e) 
          {
             string cmdText = e.CommandName;
          }
    

    【讨论】:

    • 勾选我的答案,如果可行,请为我的答案投票:)
    猜你喜欢
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    • 2021-12-06
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多