无法将类型 'System.Web.UI.WebControls.GridView' 的对象转换为类型 'System.Web.UI.WebControls.LinkButton'。使用分页时

Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.LinkButton'. while using pagination

我有一个 Gridview,我在其中放置了两个 LinkBut​​tons 用于编辑和删除行。

<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 页...时,它给了我错误:-

Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.LinkButton'.

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

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

更改视图如下:

<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"/>

更改代码隐藏如下:

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

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

(LinkButton)e.CommandSource

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

另一件事,e.CommandArgumentgridview_RowCommand returns 行的索引,而不是控件 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");
}
}