GridView question

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

I have a gridview that has Edit, Delete, and Select enabled. I'm
trying to run the below C# to prompt when deleting.

protected void DetailGridView_RowCreated(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton l = ((LinkButton)e.Row.Cells[0].Controls[1]);
l.Attributes.Add("onclick", "javascript:return " +
"confirm('Are you sure you want to delete the " +
DataBinder.Eval(e.Row.DataItem, "SOCDetail") + " SOC
Detail Entry?')");
}
}

When the control renders I'm getting the below Error:

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

What am I doing wrong?

Thanks
 
I have a gridview that has Edit, Delete, and Select enabled. I'm
trying to run the below C# to prompt when deleting.

protected void DetailGridView_RowCreated(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton l = ((LinkButton)e.Row.Cells[0].Controls[1]);
l.Attributes.Add("onclick", "javascript:return " +
"confirm('Are you sure you want to delete the " +
DataBinder.Eval(e.Row.DataItem, "SOCDetail") + " SOC
Detail Entry?')");
}
}

When the control renders I'm getting the below Error:

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

What am I doing wrong?

Apparently it's that you're assuming that the link button is the second
control in the first cell when it seems that that's not the case. Maybe you
should use FindControl() instead.

My best advice would be to ask in an ASP.NET group.
 
Back
Top