Header Control

  • Thread starter Martyn Fewtrell
  • Start date
M

Martyn Fewtrell

This is one that has had me puzzled for some time and although I assume that
the solution is relatively straight forward but I haven't yet seen an
example or solution or managed to work one out.

If you create a user control for header, navbar etc the hyperlinks only work
if the relative location is correct for all the pages using the user
control. I am aware that using the asp:hyperlink control you can set the
link information within the code (I'm using VB.Net). However the MapPath
statement provides a path to the drive letter which is not particularly
useful in passing to a hyperlink. I know that the ResolveURL calculates
relative URL's but I don't seem to be able to apply that to the MapPath
option.

Once again I am sure that there is a simple solution but I do find it
strange that I have never seen it implicated on any sample code etc.

All help appreciated.

Martyn Fewtrell
(e-mail address removed)
 
E

Eric Caron

Hi Martyn,

The method I use to apply the correct path for navigation components
is this:

In the global.asax file, in the Session_Start Event, I do the
following:

If (Application.Item("ApplicationPath") = "") Then
Dim strApplication As String = Request.ApplicationPath
If (Right(strApplicationPath, 1) <> "/") Then strApplicationPath
&= "/"
Application.Item.Add("ApplicationPath", strApplicationPath)
End If

Then, in your user control, you set the path on your hyperlink
controls using the stored application path in the Application
collection:

Hyperlink1.NavigateURL = Application.Item("ApplicationPath") &
"somepath/"

Note that the path in the "ApplicationPath" variable already contains
the trailing slash. It's a shame that the Request.ApplicationPath
variable doesn't return a properly terminated path (with a slash at
the end) when you're working with a virtual directory. It would have
been much simpler to use Request.ApplicationPath directly in the user
control instead.

HTH

Éric
 
M

Martyn Fewtrell

Eric

Thanks for your response.

It seems surprising that this must be a very common issue although I have
not found much information regarding the problem. Since posting the message
I have found that

hyperlink.NavigateUrl = ResolveUrl("../mypage.aspx")

seems to work. I certainly haven't tested this extensively but it seems OK
on my development machine.

Martyn Fewtrell
(e-mail address removed)
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top