Problem with Gridview on RowCommand event

Posted by Madhuri123 under Regular Expressions on 7/9/2011 | Points: 10 | Views : 10322 | Status : [Member] | Replies : 2
Hi,
I am getting error while handling with gridview. i.e.., i write edit and delete operation on Rowcommand,it is working well,after that i write a code for paging ,for paging code see below .when u click on edit button its working well,but when u click on paging i am getting these errors


Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.LinkButton'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.LinkButton'.

Source Error:


Line 137: protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
Line 138: {
Line 139: GridViewRow grow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
Line 140: Label ITCategoryId = (Label)GridView1.Rows[grow.RowIndex].FindControl("ITCategoryId");
Line 141:


code


protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow grow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
Label ITCategoryId = (Label)GridView1.Rows[grow.RowIndex].FindControl("ITCategoryId");

if (e.CommandName == "Edit")
{

Response.Redirect("ClassifieldsCategory.aspx?Category_id=" + ITCategoryId.Text);

}
}

Paging Code

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


Can u please help me


Thanks InAdvance

T.Madhuri




Responses

Posted by: Umeshdwivedi on: 7/9/2011 [Member] Starter | Points: 25

Up
0
Down
Hallo,
will fire only edit command click event other wise not fire so error cannot come.
if not work ask again
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

{
if (e.CommandName == "Edit")
{
GridViewRow grow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
Label ITCategoryId = (Label)GridView1.Rows[grow.RowIndex].FindControl("ITCategoryId");
Response.Redirect("ClassifieldsCategory.aspx?Category_id=" + ITCategoryId.Text);
}
}



Latest Technology Trainer
And Part time software consultant

Madhuri123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muralikrishna on: 10/16/2012 [Member] Starter | Points: 25

Up
0
Down
Yes.......if already u used the CommandName and CommandArgument values for other purposes u can check this way..............

if (e.CommandName.ToString() == "Page")
{

}



Madhuri123, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response