mike
mike

Reputation: 21

a problem with text displaying in HyperLink control

I am using ASP.NET 2.0

and I want my hyperlink should display with an image and the Text that i want.

like this

<Image>Hyperlink

But if I'm setting the ImageUrl in asp:hyperlink control then it only displaying the image not the text.

How can I do the specified thing.....

Upvotes: 2

Views: 4546

Answers (4)

Damjan
Damjan

Reputation: 49

If you define both ImageUrl and Text attributes of the asp:Hyperlink class, the ImageUrl takes precedence and you can't use the Hyperlink class as a control container.

To have both text and image in the same link use a HtmlAnchor. Ex.

     <a ID="htmlAnchor" runat="server" href="~/yourpagehere.aspx">
       <asp:Label ID="lblText" runat="server" ></asp:Label>
       <asp:Image ID="imgage" runat="server" ImageUrl="~/images/myimage.png"  />
    </a>

Upvotes: 5

Kirit Chandran
Kirit Chandran

Reputation: 719

Alternative method: You could use an image button. The On click method can be dynamically setup on the back end as well to re-direct to a URL using btnView.Attributes. Just another approach.

<ASP:IMAGEBUTTON id="btnView" onclick="btnView_Click"                   runat="server" imageurl="../images/buttons/view.gif"></ASP:IMAGEBUTTON>

Upvotes: 0

serhat_pehlivanoglu
serhat_pehlivanoglu

Reputation: 982

i am not sure if it will act like the same but, you can make a asp:Linkbutton show both image and text.

basicly you give the image as a background to the control via css.

http://forums.asp.net/p/1024015/1391402.aspx

But i can not try it right now so again, i am not really sure how will it act with hyperlink.

Upvotes: 0

Fr&#233;d&#233;ric Hamidi
Fr&#233;d&#233;ric Hamidi

Reputation: 263037

The ImageUrl property, if specified, takes precedence over the link's text. To render both image and text, you need to put an Image element inside your hyperlink:

<asp:HyperLink runat="server" NavigateUrl="foo.aspx">
    <asp:Image runat="server" ImageUrl="bar.png" />
    (link text)
</asp:HyperLink>

Upvotes: 1

Related Questions