Question

~/Folder1/UserControl1.ascx:

<%@ Control Language="C#" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.WebUserControl1" %>
<asp:HyperLink runat="server" NavigateUrl="?foo=bar">HyperLink1</asp:HyperLink>

~/UserControl2.ascx:

<%@ Control Language="C#"CodeBehind="WebUserControl2.ascx.cs" Inherits="WebApplication1.WebUserControl2" %>
<asp:HyperLink runat="server" NavigateUrl="?foo=bar">HyperLink2</asp:HyperLink>

Result:

http://localhost/Folder1/?foo=bar

http://localhost/?foo=bar

Why does it happen?

Was it helpful?

Solution

WHen you do not have a / or ~ at the beginning of the path, it is considered relative to the CURRENT position.

From a user control, current position is the position of the control.

OTHER TIPS

The links are resolve relative to the directory of the user control, they actually call ResolveClientUrl(); internally, so you see this same behavior.

Description from MSDN:

The URL returned by this method is relative to the folder containing the source file in which the control is instantiated. Controls that inherit this property, such as UserControl and MasterPage, will return a fully qualified URL relative to the control.

Resolving the urls with Page.ResolveClientUrl() in the code-behind will solve the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top