link URL confusion

K

Keith G Hicks

I'm confused about how to code a link button to go to the page I need
(asp.net 2.0)

My root is "Website1"

Here's the layout for the purposes of this question:

Website1 (folder)
masterpage.master (in the root)

Admin (folder off the root)
SendMail.aspx (uses the master)

LoggedInUsers (folder off the root)
EditLoginInfo.aspx (uses the master)

Default.aspx (this is in the root and also uses the master)

Most of the pages are in the root including the master page. All the pages
in the system use that master page.

On the master page I have a line of text like this:

<a href="LoggedInUsers/EditLoginInfo.aspx">Edit Login Info</a>

When I'm on the Default.aspx page and click the link, it goes to the page I
expect it to. However, when I'm on the SendMail.aspx page and I click the
"Edit Login Info" link, I get the error: "The resource cannot be found"
Requested URL: /Website1/Admin/LoggedInUsers/EditLoginInfo.aspx

I understand why because obviously there's no such folder layout as
/Website1/Admin/LoggedInUsers/ so it's not finding the page but I don't know
what to change to make it work. If I change the link to read

<a href="~/LoggedInUsers/EditLoginInfo.aspx">Edit Login Info</a>

I get the same error: "The resource cannot be found"

and:

Requested URL: /Website1/Admin/~/LoggedInUsers/EditLoginInfo.aspx

Now what's really confusing to me is if I use a LinkButton instead of just
text with href, and set the PostBackUrl in the button as follows:

PostBackUrl="~/LoggedInUsers/EditLoginInfo.aspx"

then that link button works fine no matter where I am in the site. Doesn't
matter what folder I'm in, it always finds the page I need.

Do I have to use link buttons with PostBackUrl whenever I'm dealing with
master pages and subfolders or is there a way to use href in text and get
this to work right?

I hope I've made the problem clear enough.

Thanks,

Keith
 
K

Keith G Hicks

I should point out that if I put the root in the <a href ... like this:

<a href="/Wesite1/LoggedInUsers/EditLoginInfo.aspx">Edit Login Info</a>

It works fine, but everthing I read says it's bad practice to hardcode the
root anywhere.

Keith
 
J

Jesse Houwing

Hello Keith,

You can use the <asp:hyperlink /> control to add a link to a different page.
it has a property in which you can set the url using the ~/path notation.
The hyperlink control will automatically resolve the url to teh correct value
depending on which page is using the masterpage. Just like your LinkButton
is doing. Another thing you could try is to set the runat="server" attribute
on your <a href=".."> tags.
 

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,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top