Using hyperlinks in ASP.NET

Hello Techies,

Now a day I am working on a project, in which I used master page. Initially I used html hyperlink in the pages, and I found it is not as easy as it seems in the blog tile (“Using hyperlinks in ASP.NET”). I came across a problem in dealing with hyperlinks. I would like to share the problem and its solution with you all.

Problem:
If we use html hyperlink in asp.net, it creates problem.

Problem Replication:
1. Create an asp.net web application project, HyperLinkError. Choose the language of your preference.
2. Web application will have Default.aspx file by default. Delete it.
3. Add Master page to the application
4. Add new web form in the application, Default.aspx. Make sure you select master page for it.
5. Write “Default Page” in Default.aspx content place holder to recognize default page.
6. Create a folder inside the project, let’s say “User”
7. Add form in User folder, let’s say UserDefault.aspx. Make sure you select master page for it.
8. Write “User Default Page” in UserDefault.aspx content place holder to recognize user default page.
9. Put following html in the master page. It will create two hyperlinks in the master page



10. Set Default.aspx as start page by right clicking the page in solution explorer.
11. Run the web application.
12. Web application will look similar to this:



13. Notice web URL http://localhost:xxxx/HyperLinkError/Default.aspx
14. Click on Home hyperlink, the same page will get displayed Ok
15. Click on "Click Me" hyperlink



16. Notice web URL, http://localhost:xxxx/HyperLinkError/User/UserDefault.aspx
17. Now click on Home hyperlink, it throws following error




18. Notice web URL, it is http://localhost:xxxx/HyperLinkError/User/Default.aspx
19. Now if you go back and click on "click me" web URL changes to http://localhost:xxxx/HyperLinkError/User/User/UserDefault.aspx showing above error
20. That means, if URL is changed to some other folder, it assumes it as root folder and tries to find pages at that path.

Resolution:
Replace HTML hyperlinks with asp.net hyperlink controls. Assign its Navigation URL as below. All is set, try running the application again.



Since, here is the restriction for attaching files, I can not attach dummy application for you. If you need dummy application for understanding the problem, mail me. I will back you back with the source files.


Thanks
Sanjivani

Comments