I am using Master and Content Pages, now I have a situation that I dont want to use the css of Master page on Content Page.
There are alots of classes and css files so overriding them is not possible I just have option to not include them in content page.
So what are the possible scenarios?
Having in mind that every equivalent method between the MasterPage and the ContentPage is being executed always later by the MasterPage (see here), we have to introduce the CSS change inside the MasterPage.
That being said, you can detect the name of the ContentPage actually being displayed inside the Masterpage using Page.AppRelativeVirtualPath.ToString() in your ContentPlaceHolder.
Then, you can modify the css inside the masterpage saving it in an asp:Literal that includes the HTML link tag.
For example:
MasterPage.aspx
<head>
<asp:Literal runat="server" ID="cssStyleSheet">
</head>
<body>
<asp:ContentPlaceHolder ID="contentPageHolder" runat="server">
</body>
MasterPage.aspx.cs
public void ModifyCSS(){
string contentPageName = contentPageHolder.Page.AppRelativeVirtualPath.ToString();
int pos = contentPageName.LastIndexOf("/") + 1;
contentPageName = contentPageName.SubString(pos, contentPageName.Length -pos);
switch(contentPageName)
{
case "Login.aspx":
cssStyleSheet.Text = #"<link rel='stylesheet' type='text/css' href='Styles/Login.css' />";
break;
case "Logout.aspx":
cssStyleSheet.Text = #"<link rel='stylesheet' type='text/css' href='Styles/Logout.css' />";
break;
}}
Related
I have an ASP.net web application, and would like to know if is it possible to change the CSS of a site at run time of a master page, and get the CSS file name from a SQL database and add it into the system ?
You can put on the top of the masterpage on aspnet literal and construct the link in the pageload of the masterpage that way you could put a css there
<asp:Literal ID="Css" runat="Server" />
Then on the page load
var cssfilename = GetCssFromDatabase();
Literal.Text = "<link rel=\"stylesheet\" media=\"all\" " + cssfilename + "\" />";
Yes it is. Use <head runat="server"> and you can put anything in there, simplest way probably a Literal control where you append the string from codebehind
Masterpage.master:
<head runat="server">
<title>Your Site</title>
<asp:Literal runat="server" ID="cssLiteral" />
</head>
Masterpage.master.cs:
cssLiteral.Text = "<link rel='stylesheet' type='text/css' href='"+strCssFileName+"' />";
You haven't provided much information of your case, yes it's possible in many ways. Here is a simple example how to change css-file in master-page from a content page:
First add a static variable for example in Global.asax (code in VB.Net)
Public Shared DefaultCssFile As String = "~/MyStyles.css"
In master pages header:
<asp:PlaceHolder runat="server">
<link rel="Stylesheet" type="text/css" href="<%= Page.ResolveUrl(MyApp.Global_asax.DefaultCssFile) %>" />
</asp:PlaceHolder>
Then just change the variables value as you see fit.
MyApp.Global_asax.DefaultCssFile = "~/MyOtherStyles.css"
There are several ways you can achieve this.
Best way I suggest you to use theme and skin file concept.
protected void Page_PreInit(object sender, EventArgs e)
{
switch (Request.QueryString["theme"])
{
case "Blue":
Page.Theme = "BlueTheme";
break;
case "Pink":
Page.Theme = "PinkTheme";
break;
}
}
here you can change the values of your theme run-time from code behind accurately. you can put your .css files into different themes. and as per database conditions you can change values of theme files.
hope this will help.
I would like to overwrite the MasterPage title. NOTE: I'm not talking about setting a portion of the title from the content page.
I have this in my MasterPage:
<head id="Head1" runat="server">
<title>My Site | <% = Page.Title %></title>
</head>
Which, in combination with this on my content pages...
<%# Page Title="Content page title" ... %>
delivers the following to the user:
<title>My Site | Content page title</title>, which is great 99.9% of the time. However, I would like to be able to change the entire title delivered to the client so that it does not include the portion specified in the head of the MasterPage, IE: <title>Special content page</title>
Is it possible to change the entire page title from the content page programmatically? I do not want to change the configuration of the MasterPage or use a different MasterPage.
EDIT: I also need to be able to implement this without changing existing content pages. Sorry I didn't state this explicitly. I assumed it would be understood
Change your master page markup to
<head id="Head1" runat="server">
<title><asp:Literal ID="MasterPageTitle" runat="server" /></title>
</head>
In your masterpage.cs
public string ExplicitTitle { get; set; }
protected void Page_PreRender(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(this.ExplicitTitle))
{
this.MasterPageTitle.Text = "My Site | " + Page.Title;
}
else
{
this.MasterPageTitle.Text = this.ExplicitTitle;
}
}
In your "Special content page" add the this to Page_PreRender
((MySiteMaster)this.Master).ExplicitTitle = "Special content title";
There are two approaches depending on which form of ASP.Net you're using.
If you're using MVC, then simple set a property called "PageTitle" in your ViewBag and then reference it in your Master Page:
In HomeController.cs
#ViewBag.PageTitle = 'Overridden Title'
In your Master Page:
<head id="Head1" runat="server">
<title>My Site | <% = #ViewBag.PageTitle %></title>
</head>
If you're using a WebForms based application, you can do this by first exposing the html title through "hybrid server control"
In your master page, change things to:
<head id="Head1" runat="server">
<title id="MasterTitle" runat="server"></title>
</head>
Then in your child pages, you add a type reference to your master page up at the top:
<%# MasterType virtualpath="~/Templates/YourMasterPage.master" %>
In your child page code behind, you now have access through.
Master.MasterTitle.Text = "Overridden Title";
For VS2012, in your child pages if using WebForms, you change absolutely nothing in your Master Page. Everything can now be done through your child pages:
The following statement goes at the very top of your child page code:
`<%# Page Title="Insert Your Title here" Language="C#" MasterPageFile="~/MyMasterPage.Master" AutoEventWireup="true" CodeBehind="SomeChildPage.aspx.cs" Inherits="SomeProject.WebForm1" %>`
In Master Page
<title>
<asp:ContentPlaceHolder ID="cphMasterTitle"
runat="server"/>
</title>
In Child Page
<asp:Content ID="contentChildTitle" ContentPlaceHolderID="cphMasterTitle"
runat="server">
<asp:literal ..... etc ... />
</asp:Content>
or without use of content/placeholder
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Title = "Child Title"
End Sub
This may be a newbie question, but i'm pretty new to asp.net & C# etc.
I'm working with an ASP.net website, and I'm curious about the structure of it (after automatically creating a web project), specifically the following:
I see that in Default.aspx , I have a tag like this:
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>**strong text**
But in Site.master, I have this:
<head runat="server">
*etc*
</head>
So where would I put code if I wanted to include JavaScript code to run, on page load?
I believe you can put your code in any of them. The first one is for adding code or script used by all content pages(that using this master page file) while the second one is if you want to to add script or code from content pages(that should be used only for this specific page)
//in the Master page, the content here is used by all content pages
<head runat="server">
*etc*
</head>
and
//this is specific to the content page that use it. This section needs to be supplied in content pages
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
That section needs to be supplied in each content page and it will be exclusive to that page - no other page can use the script in that section
asp:Content ID="HeaderContent" is a content region. Anything within that tag will get embedded in the associated ContentPlaceHolder in the master page when it is generated.
head is a standard html markup, indicating the page head elements. Typically, the HeadContent placeholder is inside the head tag on the master page.
The head element, container for all the head elements, must use a title for the document. Some other elements it can include: style, base, link, meta, script, noscript.
The asp: Content ID = "HeaderContent" is a content element of the master page.
Have a look at the Plugging in Content part of the following link for detailed information on this: http://odetocode.com/articles/419.aspx
I think you asked when you want to use JavaScript where you put JS in your code.You can put anywhere you wish in asp side between script block such as:
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script type="text/javascript">
function Onclick(){
//some codes
}
</script>
</asp:Content>
or
<head runat="server">
<script type="text/javascript">
function Onclick(){
//some codes
}
</script>
</head>
Also you can put JS outside this tag. You only should use tag.
I've got a master page setup with a contentplaceholder control inside the title tag as so:
<head id="head1" runat="server">
<style type="text/css">
body { font-family: Tahoma; font-size: 9pt; }
</style>
<title><asp:contentplaceholder id="title" runat="server" /></title>
</head>
That contentplaceholder is implemented inside a page that uses that masterpage as so:
<asp:content runat="server" contentplaceholderid="title">
Welcome: <%= this.BasketID %>
</asp:content>
I'm trying to then get a copy of the substituted title inside the masterpage body (also tried inside the page - and this doesnt work either) like:
<p>
<strong>Subject:</strong> <%# Page.Title %>
</p>
In all cases Page.Title and Page.Header.Title are "" (I've tried both databinding and using the <%= %> syntax to no avail.
Does anyone know what is going on here and how I can overcome this?
Thanks.
The problem you're getting is because you are 'tricking' the page cycle. You'd better use this in the codebehind of the page:
Master.Title = "Welcome: " + basketId
You could do it this way however; on the masterpage: create a HtmlTextWriter, configure it to write to a MemoryStream. Render the 'title' contentplaceholder to the HtmlTextWriter, attach a StreamReader to the stream to grab the content as a string, and output this to your page. Yet, this is not efficient, and way too much work :-)
I'm trying to programmatically add a <meta>. It is working fine when there is a Head element with runat = "server" in the .aspx page.
The code behind is:
HtmlMeta meta = new HtmlMeta();
meta.Name = "robots";
meta.Content = "noindex,follow";
this.Page.Header.Controls.Add(meta);
But I have some script in the head tag which contains code blocks like <% ... %>, so I cannot keep the runat = "server" value.
The problem is I have to add the meta tag programmatically, because it depends on a value from the database.
Is there a way to solve this issue so that my script inside the head element works as usual and I can add a meta tag programmatically?
OK, I tested the answer by veggerby, and it works perfectly:
In the <header> section:
<asp:PlaceHolder id="MetaPlaceHolder" runat="server" />
Note that Visual Studio might show a warning on the PlaceHolder tag, because it is not recognised as a known element inside the header, but you can ignore this. It works.
In the C# code:
HtmlMeta meta = new HtmlMeta();
meta.Name = "robots";
meta.Content = "noindex,follow";
MetaPlaceHolder.Controls.Add(meta);
Alternatively (since you already have code blocks using <% %> in your header section), you can tag the meta directly and retrieve only the value from server side:
<meta name="robots" content="<%=GetMetaRobotsValueFromDatabase()%>" />
Many thanks to Awe for the solution! I have implemented this code in a (error404.ascx) ASP.NET User Control as follows:
<%# Control Language="C#"%>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Response.TrySkipIisCustomErrors = true; //Suppress IIS7 custom errors
Response.StatusCode = 404;
SetRobotsHeaderMetadata();
}
private void SetRobotsHeaderMetadata()
{
HtmlMeta meta = new HtmlMeta();
meta.Name = "robots";
meta.Content = "noindex,follow";
this.Page.Master.FindControl("cphPageMetaData").Controls.Add(meta);
}
</script>
With the following masterpage:
<%# Master Language="C#" AutoEventWireup="true" Inherits="MyMaster" %>
<script runat="server">
...
</script>
<!DOCTYPE html>
<html lang="en-GB">
<head>
<title>Site title here</title>
<asp:contentplaceholder runat="server" id="cphPageMetaData">
</asp:contentplaceholder>
</head>
<body>
...
</body>
</html>
Or you could just put your meta-tag in the header, with an ID and a runat="server"... then in the code behind say
myMetaTag.Content = "noindex,follow";
or
myMetaTag.Visible = false;
or whatever you'd like.
I think this is the best approach:
this.Page.Header.Controls.Add(new LiteralControl(#"<meta ... />"));
Enjoy!
Try moving whatever it is that you are doing in the <% .... %> to the code-behind. If you are using the script to add content into the page, you can replace it with an asp:Literal control and then set the value you were previously calculating in the script block to the code-behind and set Literal.Text to that value.
I haven't tested it, but maybe you can add an <asp:Placeholder> inside the <head></head> tag and add the meta tags to this.
The best solution for this, which I successfully checked without any error or warning:
The JavaScript code, which contains the <% ... %> code, was removed from the head section and placed in the body section.
You could define your meta tag as a static string like so:
Private Shared MetaLanguage As String =
String.Format("<meta http-equiv=""Content-Language"" content=""{0}""/>", CultureInfo.CurrentUICulture.TwoLetterISOLanguageName)
Then place them in your head like so:
<head runat="server">
<%=MetaLanguage%>
</head>
This allow you to use any meta tag values and is easy to read and customize. Note: The use of the Shared keyword (static) helps improve performance.
MetaDescription = "Your meta description goes here";
MetaKeywords = "Keyword1,Keyword2,Keyword3";
OK... I actually only use C#... Or HTML into C#. I never use codebehind, designer or webcontrols in the file aspx... So I program everything from classes... And dynamically.
Result:
HtmlMeta meta = new HtmlMeta();
meta.Name = "robots";
`meta.Content = "Here is what you want";`
var page=HttpContext.Current.Handler as Page;
page.Header.Controls.Add(meta);