can not resolve style from master page's pageload - asp.net

I have a page login.aspx in a folder which is linked to masterpage. In the page load event of masterpage I have added some styles. When I redirect to login.aspx, it is just not able to get the styles from the masterpage's pageload event. I analysed the problem found that because my login.aspx is not in root folder, but in a folder which is inside root folder.
How do I run masterpage's pageload event in login.aspx?

You can place all your style sheets in a folder structure as follows:
App_Themes/Style/mystylesheet.css
Then in your content ASPX pages, just add Theme="Style" to the page directives and ASP.NET will automatically resolve it for every page that you have :-)

I assume you're talking about CSS stylesheets, and not ASP.NET styles (themes).
In that case, you can using a tag like the following from your Master page:
<link runat="server" rel="Stylesheet" href="~/scripts/common.css"
type="text/css" />
Or you can insert the same tag programmatically from your Page_Load() handler. However, in that case, it's best if you add the HtmlLink control to the Head control. Alternatively, you can add an ID to the control and use Visible="True" to control whether it appears in the generated markup.

If the code works when you move the code to your templatized page (not the but the one that uses it) then it suggests that you are using a relative link for the stylesheet.
I'd recommend using a relative URL off the root (something in the form of "/stylesheet.css") so that when you have pages that use the template, but in a subdirectory, it can resolve the stylesheet correctly.

The problem is that the section of the markup is located in the Master Page, so the reference to the StyleSheet cannot be made
Dim link As New HtmlLink
link.Href = "LocationOfStyleSheet.css"
link.Attributes.Add(HtmlTextWriterAttribute.Rel.ToString(), "stylesheet")
Page.Header.Controls.Add(link)

Related

Image from Master Page Disappears when Putting .aspx Page in a Folder

I know this is probably a stupid question but I can't find anything on here about it. I have a master page in the root of my solution explorer and just put an .aspx page in a new folder. The master page is referencing an image in another folder and when I preview the page, it's a broken image. What do I have to do to have the page load the image from a master page? Thanks
You must use relative path. Below is an example:
<asp:Image ImageUrl="~/Images/orderedList1.png" runat="server" />
<img src="~/Images/orderedList0.png" runat="server" />
Don't forget to add runat="server" if you don't use asp.net control
Well, by not posting the markup with the reference to the image, you leave us to guess what's happening.
But how about this: you only have a partial (relative) path to the image, and that path is not valid from the location of your aspx file.
Did I guess right?
You may be referencing the image with no path.
When a child page is loaded in a MasterPage, the "current" directory changes to the child page's directory. So the relative path has changed.

what is the masterpage on this asp.net webformpage?

I have a asp.net webformpage without a MasterPageFile property at the top but I have asp:content tags in there. I assume this page has a masterpage what is the masterpage in this case?
Check web.config maybe there is a global master page define in the <page> element there.
Also, check the code-behind. Maybe it's declared in code.

asp.net derived master pages, page titles and dynamic css links

Not sure if the derived page is actually relevant to the problem here, but ran into an interesting gotcha on some code I'm working through at the moment.
I have a custom masterpage class, which derives from System.Web.UI.MasterPage so that it can be extended with additional useful properties.
The page that that uses this masterpage has a declaration at the top (note the Page Title being set).
<%# Page Language="C#" MasterPageFile="~/MasterPages/Landing.master" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" Title="Welcome to the site" %>
In addition, the master page has stylesheet references in the head which are pulled from a CDN that is defined in a config file.
<head id="Head1" runat="server">
<link rel="stylesheet" type="text/css" href="<%= CDN %>/css/main.css" />
</head>
Now the example above obviously doesn't work, because the runat attribute in the head container means that the codeblock in the the link is static text, and renders as is, in the resulting html.
If I remove the runat attribute from head, the CDN works, but now I notice that the Title is no longer being set. If I debug, and try to access Page.Title in the Immediate Window, I get an exception:
// Using the Title property of Page requires a header control on the page. (e.g. <head runat="server" />).
So, is there a way to get the Page Title from the declaration, put my own title placeholder in the head and set it from the master page code-behind, or, is there a better way to dynamically set the CDN domain for the stylesheets? The only way I think I can do that is to build the entire html link tag(s) and append it to the header control, but I thought there might be a more elegant solution, so I'm asking here first.
Insofar as the CDN goes, we build a special Url.ResolveContentUrl() method that spoke with our configuration and pointed at the correct stylesheets (and any other static asset) depending on running mode, etc. So your dev and QA work locally then your production goes to the CDN with zero code changes.
Also, you should put a content placeholder into the part of the masterpage. It is the only way to fly.
In your masterpage, you can create a routine like this:
Public Sub AddStyleSheetLink(ByVal fileName As String, Optional ByVal media As String = "all")
Dim stylesheetLink As New HtmlLink
With stylesheetLink
.Attributes("href") = fileName
.Attributes("type") = "text/css"
.Attributes("rel") = "stylesheet"
.Attributes("media") = media
End With
MasterHeader.Controls.Add(stylesheetLink)
End Sub
And in your Page_Init (the page that inherits your masterpage), so something like this
CType(Master, MasterPage).AddStyleSheetLink(CND & "/css/main.css")
You can dynamically add what you need without messing with your <head> tags and page title.

master page has runat=server on the head tag element, it is injecting a stylesheet using themes?

master page has runat=server on the head tag element, it is injecting a stylesheet on the page for some reason and I am not doing it explicitly.
Is it a themes setting that is adding the stylesheet? How can I disable it for this master page?
Yes I would suspect a theme, to remove it:
Try EnableTheming="false" in your
masterpage header.
If you don't want themes at all look
for <pages theme="Name of theme goes here" /> attribute in your
web.config file and comment it out.

Usercontrol access Javascript from a Content's Page's Master Page

Hello all I have a problem. I have a masterpage that all of my Content pages inherit from. Within this masterpage I have a script tag pointing to the Javascript file folder ~/Scripts/validation.js
On my content pages I use different usercontrols that require the use of many of the functions within the validation.js file however if I dont put the <script> tag and the Javascript functions within a contentholder on the contentpage the usercontrols do not see these functions and I get errors like OnNameValidation is not defined.
Of course I can copy the Javascript code into all of the pages but that's 30+ pages and a maintenance nightmare if I find a bug in one of the Javascript functions.
So the question (if you haven't already figured out from my long dissertation) is how can I declare the script tag with the path to the validation.js file so that contentpages and their usercontrols etc. can access the functions/code.
What you are trying to do should work, so I suspect that the path to your javascript file is wrong (without seeing your html code I can only assume). Keep in mind that you can only reference the javascript file like this: "~/Scripts/validation.js" if you have the link in a HEAD runat="server" tag. Without the runat="server" it won't find the file. You would have to do something like "../scripts/validation.js"
As a test I would try to call your javascript function in the masterpage, so you can rule out a bad file reference.
I picked up this tip from ScottGu here.
Add this to you user control which enables Intellisense in user controls but always evaluates false:
<% if (false) { %>
<script src="/Scripts/validation.js" type="text/javascript"></script>
<% } %>
I am currently doing this in my site by going to the Source code on the master page and putting the following in the head and outside of the ContentPlaceHolder.
<head>
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
<script src="CP.js" type="text/javascript"></script>
</head>
The path you are assigning for your js file is probably not matching in all the pages.
script src="../JavaScript/Scriptaculous/scriptaculous.js"
It should have something like above this if you have separate folder for Scripts, MasterPages, Pages & Controls.

Resources