How to set width and height of image using HyperLink Control?

  • Thread starter Robson Carvalho Machado
  • Start date
R

Robson Carvalho Machado

Dear friends,

I'm dynamically creating a Hyperlink with spacer.gif as ImageURL that is an
1px transparent image only to determine link position, but as I create this
link dynamically I could not set image width. This problem makes my link
clickable only on its borders.

When viewing HTML source I could see that spacer.gif has no width and
height, so HTML shows only 1px x 1 px image.

What can I do to make all the area clickable?

CODEBEHIND
-----------------
Dim myHyperLink as HyperLink
myHyperLink = New HyperLink
myHyperLink.ID = "link1"
myHyperLink.ImageUrl = "spacer.gif"
myHyperLink.NavigateUrl = "default.aspx"
myHyperLink.Attributes("style") =
"style="border-width:1px;border-style:Dotted;height:40px;width:128px;Z-INDEX:
103; LEFT: 408px; POSITION: absolute; TOP: 208px"
PlaceHolder1.Controls.Add(myHyperLink)

HTML SOURCE GENERATED BY DYNAMIC LINK
 
D

Dale

Though I haven't experienced your problem, I think it is clear that the
framework is not rendering the control in a fashion that will work for you.
You could either create your own control or use HTML controls marked as
server controls to accomplish the same thing. You might even use DHTML on
an HTML IMG to simulate a hyperlink by setting the cursor to hand and adding
an onclick event.

Hope this helps,

Dale Preston
MCAD, MCSE, MCDBA
 
R

Robson Carvalho Machado

Dear Dale,

As you can see, a friend on this forum has answered my question, but his
answer only solved part of the problem.

INDICATED SOLUTION
--------------------------
Dim img As New WebControls.Image
img.ImageUrl = "/corretoresdeseguros/images/spacer.gif"
img.Width = Unit.Pixel(100)
img.Height = Unit.Pixel(100)

My links are placed by a Function named AddControls.

By using indicated solution only the last HyperLink placed receives
img.width and img.height, but I need to place more than one Link by using
this solution and I don’t know how to adjust this solution to be dynamic.

Can you help me with the correction of the below code to make it work?

Public Class dynamic
Inherits System.Web.UI.Page
Dim myHyperLink As HyperLink
Dim img As New WebControls.Image

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
AddControls(“link1â€,
“border-width:1px;border-style:Dotted;height:40px;width:128px;Z-INDEX: 103;
LEFT: 408px; POSITION: absolute; TOP: 208pxâ€, “default.aspxâ€)
AddControls(“link2â€,
“border-width:1px;border-style:Dotted;height:40px;width:128px;Z-INDEX: 103;
LEFT: 488px; POSITION: absolute; TOP: 208pxâ€, “services.aspxâ€)

End Sub

Sub AddControls(ByVal ObjectName As String, ByVal Style As String, ByVal
NavigateUrl As String)
myHyperLink = New HyperLink
myHyperLink.ID = ObjectName
img.ImageUrl = "/corretoresdeseguros/images/spacer.gif"
img.Width = Unit.Pixel(100)
img.Height = Unit.Pixel(100)
myHyperLink.Controls.Add(img)
myHyperLink.NavigateUrl = NavigateUrl
myHyperLink.Attributes("style") = Style
PlaceHolder1.Controls.Add(myHyperLink)
End Sub

End Class
 
D

Dale

The problem in your code is you're adding the img to the Controls collection
of the hyperlink. The hyperlink is not a container control and will not
render child controls.

Create your hyperlinks as literal controls and add your images within the
literal control string. Add the litereral controls to the placeholder.

Dale
 
R

Robson Carvalho Machado

Hi Dale,

I couldn't realize this idea.

Can you please give me an example?

Regards
Robson Machado
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top