简体   繁体   中英

How to specify path relative to current Master Page File

I am new to asp.net but Master Pages seem to do what I need - allow me to configure different templates/presentations/styles in runtime, so my app may take on a different look and feel by selecting a different Master Page. Each Master Page sits in its own folder with a set of images tailored for that "template" - I then want, in content pages, to reference various images sitting in the folder of the currently selected Master Page, for example the navigation buttons in a DataPager:

<asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" 
    PageSize="25">
<Fields>
    <asp:NextPreviousPagerField ButtonType="Image" ShowFirstPageButton="True" 
        ShowNextPageButton="False" 
        FirstPageImageUrl="<<Current Master Root>>/img/action-first.png" 
        PreviousPageImageUrl="<<Current Master Root>>/img/action-prev.png" />
    <asp:NumericPagerField ButtonCount="10" NextPageText="&gt;&gt;" 
        PreviousPageText="&lt;&lt;" />
    <asp:NextPreviousPagerField ButtonType="Image" ShowLastPageButton="True" 
        ShowPreviousPageButton="False" 
        LastPageImageUrl="<<Current Master Root>>/img/action-last.png"
        NextPageImageUrl="<<Current Master Root>>/img/action-next.png" />
</Fields>
</asp:DataPager>
TEST: "<%= System.IO.Path.GetDirectoryName(MasterPageFile) %>" 
(this does not work if we try to use it inside the DataPager?)

Is there a proper way to be doing this, like the tilda ~ character is used to get to the application root folder? Setting this in code is also proving to be tedious, especially for things like DataPager where the properties are nested in dynamic lists of sub controls (For example I do not know how to set the property DataPager1.Fields[0].NextPreviousPagerField.FirstPageImageUrl in code).

I have found trying to do this with Themes requires more coding than I started with because I have to include the entire control definition in the skin file, not just the image url properties, and then I have to duplicate that code in every single theme/skin. If I happen to want a DataPager with a different configuration of buttons and/or numeric fields then I have to duplicate those variations in every skin file also. If I make a change at a later date I will have to remember to make that change in all skins also - seriously defeating the purpose of separating the code from the skin.

My application runs multiple projects, and each project may select its own template. For the entire application I want to say: if the current project uses TemplateX then use images from folder /TemplateX/img/, if TemplateY is seletected then use images from /TemplateY/img/. As you know CSS cannot always be relied upon to determine what image to use where, so we have to be able to put the image reference directly in the HTML output.

In the custom ISAPI DLL web system I developed I simply use a substitution string like "[#HTML.Template]/img/prev.png" to stick the template folder in front of the image URL. Is there perhaps a way I can hook a custom .aspx preprocessor to manually substitute all ocurrences of, as in the example above, "<< Current Master Root >>" with the currently selected folder?

尝试使用像这样的东西

var path = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpContext.Current.Request.ApplicationPath;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM