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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Images in datgrid

Status
Not open for further replies.

peekay

Programmer
Oct 11, 1999
324
0
0
ZA
I am doing a property website in which I wish to show photos and data of the property. I had thought that the datagrid with a template column for this would do the trick. I cannot however get the grid to show the image instead of its url. Can anybody help.
Thanks

PK Odendaal
 
Use something like:

<asp:TemplateColumn>
<ItemTemplate>

<img src="images/<%# Databinder.Eval(Container.DataItem, "yourimagename") %>.jpg">

</ItemTemplate>
</asp:TemplateColumn>

Then in the DB you just need to save the name of the .jpg in the field.

or..are you retieving from sql server database?

HTH
 
sirjon

Thanks

I am retrieving the image path from an Access database and I need to set up or change the images dynamically as you know. Is there no way I can do it in code behind ?



PK Odendaal
 
This might be useful for you if you need to use a link on your images. If you don't use it as a hyperlink, there probably are other alternatives, or you can 'protect' your image so that it does not function as a hyperlink and not need a NavigateURL. ImageURL below can be dynamically defined in the code-behind

<asp:TemplateColumn SortExpression="YourImageSort" HeaderText="YourImageHeaderText" HeaderImageUrl="Includes/YourImage.gif">
<ItemTemplate>
<asp:HyperLink id="yourimage" runat="server" ImageUrl="Includes/YourImage.gif" Width="15px" Text='<%# DataBinder.Eval(Container, "DataItem.Your_Image") %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateColumn>

---code-behind---
HyperLink objyourimage = null;
objyourimage = (HyperLink)e.Item.FindControl("Your_Image");
if (objyourimage != null) {
objyourimage.NavigateUrl = strPathName;
objyourimage.ImageUrl = "Includes/YourImage.gif"
}

Watch out for a 'gotcha' though if you have the Image show on the Header - in my case it obtained the same properties of the preceding default Command button so I had to kludge my way around this bug. Probably no problems if you do not put your image in a grid Header.

 
DLLHell

Thanks for your contribution. However it seems that your code is in C# and I use VB.Net (sorry) and I do wish to use the image as a link to some other page.


PK Odendaal
 
Then the only 3 lines that need to be in VB.NET would be these:

HyperLink objyourimage = null;
objyourimage = (HyperLink)e.Item.FindControl("Your_Image");
if (objyourimage != null) {

These could be converted to VB.NET with a little looking around. The ImageURL & NavigateURL code would be the same in VB.NET or maybe in a WITH, I am sure.

Good luck.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top