Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using an image in a data grid

Status
Not open for further replies.

MMIMadness

Technical User
Feb 10, 2004
50
GB
hi all,

i have a data grid with one of the columns getting it feed from a TRUE/FALSE data field.

what i want to do is display a set image when the value is true and no image when it's false.

i have made the column a template field and added the image to the field, with the view of turning it on and off in code.

Item template
Code:
<ItemTemplate>
   <asp:Image ID="Image1" runat="server" Height="17px" ImageUrl="yesno.jpg" Width="19px" />
</ItemTemplate>

Event code
Code:
Public Function yesno(ByVal strdate As String) As String
   Dim image1 As Image
   image1 = CType(FindControl("image1"), Image)
   If strdate = True Then
      image1.Visible = True
   Else
      image1.Visible = False
   End If          
End Function

but as yet just can't crack it. Any ideas would be great.
 
when you step through your code is image1 null or it FindControl working?

in what event does Function yesno reside?
 
opps, a little to much editing on my behalf.



Code:
<ItemTemplate>
   <asp:Label ID="Label1" runat="server" Text='<%#yesno(Server.HtmlEncode(eval("wireless").ToString()))%>'></asp:Label>
   <asp:Image ID="Image1" runat="server" Height="17px" ImageUrl="wireless.jpg" Width="19px" />
   </ItemTemplate>

and as far as i can make out findcontrol is working.

have only just started with .net much more used to classic :>

thanks for the swift reply.
 
ignore the second part, just tried getting a value of image1 and it came back with

Object reference not set to an instance of an object.

so image1 is returning as NULL
 
the reason your getting that error is because your trying to refer to an object inside the grid without using the itemdatabound event of the grid. if you wish to write your own sub and call it, you will have to do something like this..

Dim dgItem As DataGridItem
Dim ck As CheckBox
Dim Img As Image

For Each dgItem In YourGrdi.Items
ck = dgItem.FindControl("ControlName")
Img = dgItem.FindControl("YourImageName")
If ck.Checked Then
Img.Visable = True
End If
Next

something like this...this is an example of looping through the grid to check if the check box is checked, if it is then the image is visable.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top