Hi TheLearnedOne,
I think I see the problem. I'm using commandfield instead of itemtemplates with a linkbutton control.
Is there a way to reference the commandfield in the RowDataBound event?
Main Topics
Browse All TopicsI am trying to programmatically reference the delete linkbutton in a gridview using the RowDataBound event handler.
I'm getting the following error message:
Unable to cast object of type 'System.Web.UI.LiteralCont
Is the error getting thrown because my gridview in inside a panel and I need to use FindControl?
See below for my code.
Thanks for any help.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Actually, it does not appear to be the commandfield that is causing the problem.
I changed the cell index to the following:
protected void GridView1_RowDataBound(obj
{
if (e.Row.RowType == DataControlRowType.DataRow
{
LinkButton lb = (LinkButton)e.Row.Cells[2]
lb.OnClientClick = string.Format("return confirm('Are you certain you want to delete this quote?')");
}
}
I recieved the following error message now:
Unable to cast object of type 'System.Web.UI.WebControls
There is a literal control between the edit and delete buttons so '2' should be the correct index?
I discovered that the delete command field is referenced as a IButtonControl, but I'm unable to cast to a LinkButton.
protected void GridView1_RowDataBound(obj
{
if (e.Row.RowType == DataControlRowType.DataRow
{
IButtonControl ibc = (IButtonControl)e.Row.Cell
LinkButton lb = (LinkButton)ibc;
lb.OnClientClick = string.Format("return confirm('Are you certain you want to delete this quote?')");
}
}
Hmmm...that is a great question!!
You are returning a 'true' from the 'confirm'...
I know that this works:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="id" DataSourceID="SqlDataSourc
<Columns>
...
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
OnClientClick='return confirm("Are you sure you want to delete this entry?");'
Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Interesting...I got to work by changing the confirm statement to the following:
protected void GridView1_RowDataBound(obj
{
if (e.Row.RowType == DataControlRowType.DataRow
{
Button btn = (Button)e.Row.Cells[2].Con
btn.OnClientClick = string.Format("if (!confirm('Are you sure you want to delete this quote?')) return false;");
}
}
Business Accounts
Answer for Membership
by: TheLearnedOnePosted on 2009-09-16 at 14:45:44ID: 25350544
It sounds like you are referencing the wrong cell number to get the control. I would look through all the cells, and their controls to see what type of control each one has.