We help IT Professionals succeed at work.

How to display image and hyperlink in a DataList control?

mlagrange
mlagrange asked
on
Medium Priority
542 Views
Last Modified: 2012-06-27
Hello - I've got to work up a page in ASP.NET 2.0 that displays logo images (about a dozen; 3 columns by 4 rows) with corresponding hyperlinks to the company websites

I've got these image file names and company website links in a user-maintained table (in a MS Access database), and I've set up a DataList control (with RepeatColumns="3") that pulls those two fields  

I don't see a particular control that displays an image with a hyperlink; is there one?

If there isn't one, then how do you set the "NavigateUrl" property of the HyperLink control from a field in the data source? "NavigateUrl="<%# Eval("CompanyURL") %>" doesn't work like I thought.

Thanks

Mark
Comment
Watch Question

Just add a template column like the following:

<asp:DataList ID="DataList1" runat="server">
            <ItemTemplate>
                <a href='MyPage.aspx?ID<%#DataBinder.Eval(Container.DataItem,"ID_COLUMN")%>'><img src="image1.jpg" border=0></a>
            </ItemTemplate>
        </asp:DataList>

-Nauman.

Author

Commented:
Thanks nauman, that's half of it; the logo for each company needs to display along with their link.

How do I pull the file name into the <img src? I would think this would work:

     <img src='<%#Eval("CompanyLogo", "~/Images/{0}")%>' border=0>

but all I get are red x's

("CompanyLogo" is a field in the data source that holds the file name of the logo, and the logo's are in a folder called "Images")

Thanks again

Mark
<img border=0 src='~images/<%#DataBinder.Eval(Container.DataItem,"CompanyLogo")%>'>

-Nauman.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
For some reason, "'~images/" didn't work; it wouldn't work until I put the full path in there

No matter; I've got a demo shortly, so I'm happy

Thanks, Nauman.
Mark, Glad I was able to help :)

-Nauman.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.