GridView question

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
 
J

Jeff Johnson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top