Thursday, May 9, 2013

Link to Images in ASP.NET Theme folder

To link your image html tag to an image file in the asp.net theme folder, we need to create the image path. The variable part of the path is the theme name. This can be retrieved from the code in different ways:

If we have the following file organization:

App_Themes
      Default
           Images
               logo.jpg

In a class method,

namespace MyUtility
{
      class Utility
     {

                   public static string GetImageUrl(string relativeImageUrl)
                  {
                                 System.Web.UI.Page currentPage = HttpContext.Current.CurrentHandler as              System.Web.UI.Page;
                                 string url = relativeImageUrl;
                                 if (currentPage != null)
                                 {
                                      string pageTheme = currentPage.Theme;
                                      if (!String.IsNullOrEmpty(pageTheme))
                                                     url = "App_themes/" + pageTheme + "/" + relativeImageUrl;
                                 }
                                 return url;
                    }
        }
}

In ASPX
<img src="<%= MyUtility.Utility.GetImageUrl("Images/logo.jpg") %>"/>

Or simply we can do it directly in ASPX
<img src="App_Themes/<%=Page.Theme%>/Images/logo.jpg"/>

               


 Thanks,
Nerdy Geeky J!

No comments:

Post a Comment