0
votes

I have a Gridview, in which I have placed two LinkButtons for Edit and Delete rows.

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

But when I have added pagination in that Gridview and tried to go to pages 2nd, 3rd..., it gives me the error:-

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:         {

Actual Error shows in Line 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();
}
2
Please also provide the ShowAll(); function for better reviewing. - Waqar Ahmed
ShowAll(); is public method for Binding The Grid. - Limna Dsilva
What is the value of e.CommandSource when it breaks. Put a debug point on GridView1_RowCommand and reproduce error. - Nikhil Vartak

2 Answers

1
votes

Try like this and change everything accordingly and carefuly :

Change the View as following:

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

Change the CodeBehind as following:

string cmdText = e.CommandName; // Line 31

OR change your approach too as :

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

And

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

Cause of the Error is string cmdText = ((LinkButton)e.CommandSource).Text; Line and

(LinkButton)e.CommandSource

part. it's returning the Command Source which is a gridview and you are trying to convert it to a link button,so it's an invalid conversion.

An another thing, e.CommandArgument in gridview_RowCommand returns the index of the Row, not the controls CommandArgument text or value.

so, You must Try something like this

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

OR

But in this case your gridview edit button must contain CommandName property with Value "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");
}
}