HighTechTalks DotNet Forums  

LinkButton Casting error

ASP.net Web Controls microsoft.public.dotnet.framework.aspnet.webcontrols


Discuss LinkButton Casting error in the ASP.net Web Controls forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Jim McGivney
 
Posts: n/a

Default LinkButton Casting error - 12-09-2007 , 08:25 PM






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







Reply With Quote
  #2  
Old   
Nathan Sokalski
 
Posts: n/a

Default Re: LinkButton Casting error - 12-20-2007 , 11:22 PM






When controls are added to cells, there are often LiteralControls added as
well. These LiteralControls are used for extra text that has no real meaning
(for example, a space or tab used in the *.aspx file to indent lines). The
best solution is to iterate through the controls and check the type of each
one before casting. Hopefully this will help.
--
Nathan Sokalski
njsokalski (AT) hotmail (DOT) com
http://www.nathansokalski.com/

"Jim McGivney" <mcgiv1 (AT) no-spam (DOT) sbcglobal.net> wrote

Quote:
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









Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.