J
Jim McGivney
I am trying to determine the row of a Button on a DataView an. I need to
set the CommandArgument when the row is first created.
I copied the following from Visual Studio 2005 documentation
public void GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
{// The GridViewCommandEventArgs class does not contain a
// property that indicates which row's command button was
// clicked. To identify which row's button was clicked, use
// the button's CommandArgument property by setting it to the
// row's index.
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Retrieve the LinkButton control from the first column.
LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];
//Button addButton = (Button)e.Row.Cells[0].Controls[0];
// Set the LinkButton's CommandArgument property with the
// row's index.
addButton.CommandArgument = e.Row.RowIndex.ToString();
}
}
When I run it I get the following message: Unable to cast object of type
'System.Web.UI.LiteralControl' to type
'System.Web.UI.WebControls.LinkButton'.
This is an error for the line "LinkButton addButton =
(LinkButton)e.Row.Cells[0].Controls[0];"
Any suggestions would be welcome.
Thanks,
Jim
set the CommandArgument when the row is first created.
I copied the following from Visual Studio 2005 documentation
public void GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
{// The GridViewCommandEventArgs class does not contain a
// property that indicates which row's command button was
// clicked. To identify which row's button was clicked, use
// the button's CommandArgument property by setting it to the
// row's index.
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Retrieve the LinkButton control from the first column.
LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];
//Button addButton = (Button)e.Row.Cells[0].Controls[0];
// Set the LinkButton's CommandArgument property with the
// row's index.
addButton.CommandArgument = e.Row.RowIndex.ToString();
}
}
When I run it I get the following message: Unable to cast object of type
'System.Web.UI.LiteralControl' to type
'System.Web.UI.WebControls.LinkButton'.
This is an error for the line "LinkButton addButton =
(LinkButton)e.Row.Cells[0].Controls[0];"
Any suggestions would be welcome.
Thanks,
Jim