problem accessing e.Item.Cells[1].Controls[0].Text in itemdatabound

  • Thread starter Thread starter Gerald DeConto
  • Start date Start date
G

Gerald DeConto

I have a datagrid in my aspdotnet page where most columns are sortable.

I would like to be able to programatically set the ToolTip for each column
header to something like "Click here to sort by xxxxx" where xxxxx is the
column header name.

I dont want to manually hard code the xxxxx in the tooltip (pointless anyhow
if the columns are auto generated).

have tried to do a test of this via the datagrid ItemDataBound event like
this:

private void grdIncentive_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
e.Item.ToolTip = "Click here to sort by " +
e.Item.Cells[1].Controls[0].Text;
}

but VSS doesnt like the e.Item.Cells[1].Controls[0].Text (the solution does
not build and tells me:

'System.Web.UI.Control' does not contain a definition for 'Text'

HOWEVER, if I set the tooltip to some static string (ie "Foo") and debug the
page, I can set a watch variable to e.Item.Cells[1].Controls[0].Text and see
the value I want with no problem.

any help on getting this to work?? this should have taken me two minutes
and it is now going on two days.

all help appreciated.
 
Hi,

Why are you doing this on ItemDataBound event?
This event is to perform some operation in the item being "binded" not in
the header, you can do this in the PreRender event

The compiler is right also System.Web.UI.Control does not has a Text
property. That's why you cannot compile it.

but if you put a breakpoint and it happens that the element contain a
reference to a derived class of Control that does support Text you will not
get any problem. Beauty of OOP :)


Cheers,
 
You need to cast the control to a control that has Text property. For
example, if the control is a LinkButton (you should know what the control is
before hand), then you can:

e.Item.ToolTip = "Click here to sort by " +
((LinkButton)e.Item.Cells[1].Controls[0]).Text;
 
Back
Top