Link to home
Start Free TrialLog in
Avatar of MikeMCSD
MikeMCSDFlag for United States of America

asked on

asp:Image control in a DataList ItemDataBound event

I have " <asp:Image ID="imgEng" Visible=False . .> " in a DataList

I'm trying to reference it so I can set it's visible to True but can't.
This is not working,  "Image" is a problem.  in ItemDataBound event:

   Dim imgEng As Image  << not valid
   imgEng = e.Item.Cells(3).FindControl("imgEng")
   imgEng.Visible = True

Tried this but got the same problem:

   Dim imgEngAs Image = CType(e.Item.Cells(3).FindControl("imgEng"), Image)

But controls like this work:

         Dim lnkAdd As LinkButton
         lnkAdd = e.Item.Cells(3).FindControl("lnkAdd")
         lnkAdd.Visible = True

How is a asp:Image control referenced in a DataList?
Avatar of b1xml2
b1xml2
Flag of Australia image

Public Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs)
      Select Case e.Item.ItemType
      Case ListItemType.Item, ListItemType.AlternatingItem
            Dim imgEng As Image = DirectCast(e.Item.FindControl("imgEng"),Image)
      End Select
End Sub

<asp:DataList Id="DataLust1" RunAt="Server" OnItemDataBound="DataList1_ItemDataBound">

</asp:DataList>
Avatar of MikeMCSD

ASKER

Tried this but same problem:
Dim imgEng As Image = DirectCast(e.Item.Cells(3).FindControl("imgEng"), Image)
there's a slight difference:
Dim imgEng As Image = DirectCast(e.Item.FindControl("imgEng"),Image)

The CellCollection starts at 0. So the first column is e.Item.Cells(0) .. and so on and so forth. To avoid worrying about which cell the control is in, use e.Item.FindControl(..)

HTH
This is the problem:
Dim imgEng As Image   <<< it won't let me declare it . .  look at it in VS.
ASKER CERTIFIED SOLUTION
Avatar of b1xml2
b1xml2
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
where System.Drawing and System.Web.UI.WebControls are referenced within the same class, there will be a clash with the class Image (both namespaces have their respective Image class)

In intellisense inside C#, the red error icon appears when you try to type Image. This apparently is not available in the VB.NET IDE... very strange
very good, yes b1, it looks like that is the problem . . but why did they name the control
for asp:Image with namespace Image?  To drive me crazy of course.

I came up with another idea/solution in the meantime that also worked:

<asp:Image ID="imgEng" Visible='<%# DataBinder.Eval(Container, "DataItem.Engraving")%>'  . . .   the field is a bit field.