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=">>"
PreviousPageText="<<" />
<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?
try use smething like this
var path = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpContext.Current.Request.ApplicationPath;
Related
Is it possible to localize DataPager control in asp.net?
Among the fields NextPreviousPagerField can be localized since it offers properties such as FirstPageText and LastPageText. But I am having trouble localizing NumericPagerField. It basically produces the page numbers and I can't find any way of localizing those.
I have tried changing the culture of current thread, but it didn't work.
There are also properties PreviousPageText and NextPageText in NumericPagerField control that can be localized as follows (Assuming you are using Resources):
<asp:NumericPagerField
PreviousPageText="<%$Resources:TestSiteResources, PreviousPageText %>"
NextPageText="<%$Resources:TestSiteResources, NextPageText %>"
... />
Also note that:
https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.numericpagerfield.nextpagetext#remarks
The value of this property is stored in view state. It can be saved
automatically to a resource file by using a designer tool. For more
information, see LocalizableAttribute and Globalization and
Localization.
nesting of image inside a LinkButton shows image on one page & doesn't show image on another page below are two sample code from two different pages in the same root director.It works fine on one page but on the other page is doesn't show any download image rather shows the text download in place of image.
I have done troubleshooting for sometime and replace the code also but it doesnt show download image for any reason on the second page..
<asp:LinkButton ID="lnkbtnDownload" runat="server" onclick="lnkbtnDownload_Click" meta:resourcekey="lnkbtnDownloadResource1">
<asp:Image ID="imgDownload" runat="server" ImageUrl="~/images/download.png" meta:resourcekey="imgDownloadResource1" />
</asp:LinkButton>
<asp:LinkButton ID="lnkbtnDownload" runat="server" onclick="lnkbtnDownload_Click" meta:resourcekey="lnkbtnDownloadResource1">
<asp:Image ID="imgDownload" runat="server" ImageUrl="~/images/download.png" meta:resourcekey="imgDownloadResource1" />
</asp:LinkButton>
HTML OUTPUT
HTML for above two code sample render as below
<img alt="Download" src="images/download.png" id="MainContent_imgDownload">
Download
Both Pages are in the same root directory...
The problem probably comes from a discrepancy in your resource files or missing a resource file completely for the second page. Obviously you have one for the first, but possibly not for the other which has different naming.
If you are using meta:resourcekey, there are some things you have to considerate.
Make sure that your local resource files meet the following criteria:
They are in an App_LocalResources folder.
The base name matches the page name.
For example, if you are working with the page named Default.aspx, the
resource files are named Default.aspx.resx (for the default
resources), Default.aspx.es.resx, Default.aspx.es-mx.resx, and so on.
The resources in the file use the naming convention
resourcekey."property". For example, key name Button1."Text".
Source: MSDN
I have an aspx document (I know nothing about asp, .net, aspx, nada). It is a normal html table structure for the most part, but there are strings of asp that seem to be inserting some sort of dynamic content. They are in the form:
<asp:Image ID="imgTopImage" runat="server" ImageUrl="~/Images/topbar.jpg" />
<asp:Label ID="lblStyleCaption" runat="server" CssClass="label_caption" Text="Theme: " Visible="false" />
<asp:DropDownList ID="dropStyles" Width="150" runat="server" AutoPostBack="true" />
It seems that whenever I delete one of these——something as innocuous as, say, the line with the asp:Image tag, which I would think should just remove the image, when I load the page I get run-time errors. It's very particular. My question is, is this compiled somehow, which is making it so fragile. Even just changing the topbar.jpg to something.png gives me an error. Do I need to track down the original files this was compiled from, or is this normal server-side asp(x?) that I'm just somehow else goofing up my changes to?
ASPX pages are compiled, and those tags refer to objects that are known to the server, so removing them could cause errors.
First, some basics in layman's terms
Tags that begin with ASP: (Example, <ASP:Button id="btnSubmit" runat="Server" Text="Click Me" />)
are not standard html buttons. They are server controls. When generating the html that goes out to the browser, the ASP.NET runtime looks at the server controls and creates the appropriate content depending on the browser visiting the page.
In the case of the Button control, it's usually a standard html button, but the runtime also generates the JavaScript and such to handle the button's server-side click event.
Why you're probably seeing errors when you remove a control:
Quite often, there's server-side code that's written that accesses these controls. For example, the developer may have decided to change the Text or the Visible property due to some event.
If this is the case, and you remove the <asp:Button> tag, then there will be server-side code that references an object that no longer exists in the aspx page, hence the errors.
More at these links on Server Controls:
http://www.w3schools.com/aspnet/aspnet_controls.asp
(Actually, this older one is better for a new-to-asp.net developer: http://msdn.microsoft.com/en-us/library/zsyt68f1(VS.71).aspx
http://support.microsoft.com/kb/306459
I'd also recommend taking some time watching basic videos or going through the tutorials at http://www.asp.net/get-started
I just noticed this in your question:
Even just changing the topbar.jpg to something.png gives me an error.
That is a bit odd, but I know of at least one way it could happen...
Generally, Visual Studio will give you a warning (and not an error) if you include a relative URL to an image or a linked page that doesn't exist. The warning shouldn't block you from compiling. However, Visual Studio does have a setting that tells it to treat warnings as errors. That will block it from compiling. Here's how that would be set up:
from Project Settings> Configuration Properties select the build
setting and change the “treat warnings as errors” settings to true.
If you wish to NOT treat warnings as errors, simply change the setting to false.
Suppose this scenario. On my Default.aspx, I insert a context (WebForms), called MyContext.ascx, that load an image. So the path would be :
<img src="/images/hello.gif" />
Well. Now, I'd like to insert the same context on another .aspx page, that is in another forlder, such as /myfolder/MyPage.aspx
Than, the path of the image now should be :
<img src="../images/hello.gif" />
How you can see, I can't manage two different path for the same context. So, is there a way (symbol) to call the virtual-path of my application? Without using my own function as
<img src="<%=MyUtilities.GiveVirtualPath%>/images/hello.gif" />
which is boring. Who know?
Put your images in the root of your web-site (and inside a resources/styles/themes folder).
You can use the tilde ~ to indicate the root of your site. All your pages will refer to that. If you're using server side controls you do not even need to use the ResolvePath() method (in your example you should use it, if for example you wrote asp:image ImageUrl="" you do not need to. From MSDN.
This is my layout template (ascx without code behind)
<%# Control Language="C#" AutoEventWireup="true" Inherits="ws.helpers.LayoutUC" %>
<div>blah blah blah</div>
<ws:Panel runat="server" ID="left"></ws:Panel>
<ws:Panel runat="server" ID="main"></ws:Panel>
<ws:Panel runat="server" ID="right"></ws:Panel>
Modules will be added into ws:Panel later.
I also allow my user create their own ascx file to custom their page layout. And because of this i do a string replace all dangerous part like script tag (runat="server"), all asp.net html tag, <%, <%#, <#.... from their custom.
Im not worry about XSS, so dont comment on it, and ask why?
I want know your thinking about this. Is is safe? Is it scalable? Is it standard or a bad way?
Have a look at the INaminingContainer Interface http://msdn.microsoft.com/en-us/library/system.web.ui.inamingcontainer.aspx.
<asp:YourControl>
<LeftColumn>
<asp:Literal ID="literal1" runat="server" Text="User created literal" />
</LeftColumn>
</asp:YourControl>
In the .ascx from the users, they register your control and insert asp.net code into properties. In the 'YourControl' class you create placeholders and insert the markup set to a specific property into these placeholders. (e.g. everything between <LeftColumn> and </LeftColumn> will the inserted into
<asp:Placeholder ID="PlaceholderLeftColumn" runat="server"/>
Edit: I summed some of the TemplateContainer issue up and posted it here: http://www.tomot.de/en-us/article/2/asp.net/how-to-create-an-asp.net-control-that-behaves-as-a-template-container-to-nest-content-via-markup
You are allowing user-uploaded content; this is inherently unsafe and there are whole books dedicated to best practices. Given that you are doing it anyway, as long as you make sure you scrub the input, is it scalable? You are allowing creation of user-uploaded files on your site. How many will there be? How many users? What about load-balancing? This solution will not scale for many users, files, or servers.
It sounds like you are trying to create a simple CMS. Why not use one that exists currently, or adopt parts of an open source solution?