Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Relative Urls in user controls

Total Hit ( 2714)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


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:

Click here to copy the following block
Public Property ImageUrl() As String
  Get
    Return InnerImageCtl.ImageUrl
  End Get
  Set(ByVal Value As String)
    InnerImageCtl.ImageUrl = Value
  End Set
End 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:

Click here to copy the following block
Public Property ImageUrl() As String
  Get
    Return InnerImageCtl.ImageUrl
  End Get
  Set(ByVal Value As String)
    InnerImageCtl.ImageUrl = Page.ResolveUrl(Value)
  End Set
End Property



Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.