ItemDataBound problem

G

Gsnke

---------------------------------
I'm getting this error:

Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error:


Line 67: if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
Line 68: {
Line 69: string sImageID =
(((System.Data.Common.DbDataRecord)e.Item.DataItem)["ImageID"]).ToString();
Line 70: if(sImageID.Length < 1)
Line 71: {

---------------------------------
with this code:

private void rptStoryList_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
string sImageID =
(((System.Data.Common.DbDataRecord)e.Item.DataItem)["ImageID"]).ToString();
if(sImageID.Length < 1)
{
((System.Web.UI.WebControls.Image)e.Item.FindControl("imgIT")).Visible
= false;
}
}
}
 
A

Alvin Bruney

no it's not. it is expecting one type, and another type is coming back so it
doesn't know what to do and falls over dead. a cursory examination of the
code reveals to me that find controls returns a type control. you need to
then take the control object and cast it to suitable type like image or
whatever it is you are after. The preceeding only fails if you have a
template column. I can't tell what you have. in that case, your code would
be correct because the appropriate type would be returned without the need
to cast.

wait a minute...
(((System.Data.Common.DbDataRecord)
that's not a control. you can't cast to that. so this is the problem. i'll
use a label control to explain. If you did a findcontrol("mycontrol") it
would return a control of type label assuming your template column has label
controls in it. then you extract the info out of the label
ex:
Label t = (Label)e.item.FindControl("mycontrol");
if(lbl != null)
t.text = "new text here";

make sense?

--
Regards,
Alvin Bruney
Got tidbits? Get it here...
http://tinyurl.com/2bz4t
Gsnke said:
---------------------------------
I'm getting this error:

Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error:


Line 67: if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
Line 68: {
Line 69: string sImageID =
(((System.Data.Common.DbDataRecord)e.Item.DataItem)["ImageID"]).ToString();
Line 70: if(sImageID.Length < 1)
Line 71: {

---------------------------------
with this code:

private void rptStoryList_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
string sImageID =
(((System.Data.Common.DbDataRecord)e.Item.DataItem)["ImageID"]).ToString();
if(sImageID.Length < 1)
{
((System.Web.UI.WebControls.Image)e.Item.FindControl("imgIT")).Visible
= false;
}
}
}
 
G

Gsnke

Ok, how would I then grab the data ("ImageID") from the datasource and
evaluate whether to show that image?

Thanks
 

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

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top