Relative Urls in user controls

Urls that you set in user-control’s child controls are relative to the user control’s directory, not to the host page’s directory. Say for example that you have an user-control located under the /UserControls directory, and the host page under the / root directory. The user-control exposes a custom public property named ImageUrl, that allows the host page to set the image to show in some area of the user control – to do this the control typically simply maps the ImageUrl of an inner Image control. The programmer correctly expects to set the ImageUrl with an Url relative to the host page, that’s the intuitive way to do, right? However, there will be problems if the ImageUrl property is defined as follows:

Public Property ImageUrl() As String    Get        Return InnerImageCtl.ImageUrl    End Get    Set(ByVal Value As String)        InnerImageCtl.ImageUrl = Value    End SetEnd Property

In fact, the passed Url will be considered relative to the user control’s path. So, if the developer sets the ImageUrl property to “Image1.gif” in the host page, thinking that she’s linking to the Image1.gif file located under the / root directory, the inner Image control will instead point to /UserControls/Image1.gif, namely to the file path relative to the user control’s folder. It wouldn’t be intuitive for the developer to set the Url relative to the user control’s folder, also because if this path changes, she would have to change all the Url in all the host pages. There is a solution of course, and its very easy. In the user control you can covert the Urls so that they’re relative to the host page, by using the Page.ResolveUrl method. The Set block of the ImageUrl property should then be changed as follows:

Public Property ImageUrl() As String    Get        Return InnerImageCtl.ImageUrl    End Get    Set(ByVal Value As String)        InnerImageCtl.ImageUrl = Page.ResolveUrl(Value)    End SetEnd Property

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may