0

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?

ccaring
  • 33
  • 3
  • Create a new master page which does not include the CSS in question and have that particular content page use that master page. – HaukurHaf Oct 24 '18 at 08:22
  • 1
    If this is the solution then i have an idea to create 1 Master then 2 Child Masters, so that all common will be in Master then content based css in respective Child Masters. – ccaring Oct 24 '18 at 09:42
  • 1
    I believe that's the best way to accomplish this. – HaukurHaf Oct 24 '18 at 09:50

1 Answers1

0

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;       
}}
Rubén
  • 41
  • 1
  • 9