2016-03-03 93 views
-1

我正在尝试更改我的站点主标题,具体取决于它是在dev QA还是生产环境中托管。下面是HTML:根据主机名更改div css

<div id="wrapper"> 
    <form id="form1" runat="server"> 
    <div class="wrapper"> 
    <div> 
     <div id="siteHeader" runat="server">FTP Forms</div> 
     ...other stuff 
    <div> 
    <div class="wrapper"> 
     <asp:ContentPlaceHolder id="MainContent" runat="server"> 

     </asp:ContentPlaceHolder>      
    </div> 
    </div> 
    </form> 
</div> 

在我的.css文件相关的CSS,我已经试过这没有#siteHeader为好。

#siteHeader.header { 
    background-color: forestgreen; 
    color: white; 
    font-size: 24pt; 
    font-weight: bold; 
    padding: 5px; 
    border-bottom: 1px solid #000; 
} 

#siteHeader.headerQa { 
    background-color: yellow; 
    color: white; 
    font-size: 24pt !important; // I tried important to no avail... 
    font-weight: bold; 
    padding: 5px; 
    border-bottom: 1px solid #000;  
} 

siteHeader.headerPrd { 
    background-color: red; 
    color: white; 
    font-size: 24pt; 
    font-weight: bold; 
    padding: 5px; 
    border-bottom: 1px solid #000; 
} 

这是我尝试过的东西。 CodeBehind Site.Master.cs,我已经在Page_load,Pre_Init和master_Page_PreLoad中尝试了这一点,它在我的本地主机和开发工作,但没有QA,我没有尝试Prod的显而易见的原因。另外,innerText会按照它应该改变的而不是CSS。

string hostName = Request.ServerVariables["Server_Name"]; 

    if (hostName.ToUpper().Contains("DEV") || hostName.ToUpper().Contains("LOCALHOST)) // tested on local like this for each if statement. 
    { 
     siteHeader.InnerText = "FTP Forms -Development"; 
     siteHeader.Attributes["class"] = "header"; 
     Page.Title = "FTP Dev"; 
    } 
    else if (hostName.ToUpper().Contains("STAGE") || hostName.ToUpper().Contains("LOCALHOST")) 
    { 
     siteHeader.InnerText = "FTP Forms - QA"; 
     siteHeader.Attributes["class"] = "headerQa"; 
     Page.Title = "FTP QA"; 
    } 
    else 
    { 
     siteHeader.InnerText = "FTP Forms - Production"; 
     siteHeader.Attributes["class"] = "headerPrd"; 
     Page.Title = "FTP Prod"; 
    } 

我也试过评论说,在JavaScript中这样做也改变了内部文本而不是CSS。我甚至尝试过使用内联风格,但也无济于事。这在我的本地主机或服务器上不起作用。 localhost上else应该生效

<script type="text/javascript"> 
    var hostName = location.hostname; 
    if (hostName.indexOf("dev") > -1) { 
     document.getElementById("siteHeader").className = "header"; 
     document.getElementById("siteHeader").innerText = "FTP - Forms Development"; 
    } else if (hostName.indexOf("stage") > -1) { 
     document.getElementById("siteHeader").className = "headerQA"; 
     document.getElementById("siteHeader").innerText = "FTP - Forms QA" 
    } else { 
     document.getElementById("siteHeader").style = "background-color: red; color: white; font-size: 24pt; font-weight: bold; padding: 5px; border-bottom: 1px solid #000;"; 
     document.getElementById("siteHeader").innerText = "FTP - Forms Production" 
    } 
</script> 
+0

您可能需要彻底改变过程和使用环境具体web.configs:http://stackoverflow.com/questions/305447/using-different-web-config-in-development-and-production-environment – JoshNaro

+0

我已经有连接字符串单独的配置,你是说使用不同的site.master并指定它与web.config?或者有没有办法从web.config中定位特定的html元素? –

+0

您可以使用AppSettings来存储您的标题类和文本值。 – JoshNaro

回答

0

我的应用程序的解决方案是重写Page.Render()方法在我Site.Master.cs。

protected override void Render(HtmlTextWriter writer) 
{ 
    string hostName = Request.ServerVariables["Server_Name"]; 

    if (hostName.ToUpper().Contains("DEV")) // tested on local like this for each if statement. 
    { 
     siteHeader.InnerText = "FTP Forms -Development"; 
     siteHeader.Attributes["class"] = "header"; 
     Page.Title = "FTP Dev"; 
    } 
    else if (hostName.ToUpper().Contains("STAGE") || hostName.ToUpper().Contains("LOCALHOST")) 
    { 
     siteHeader.InnerText = "FTP Forms - QA"; 
     siteHeader.Attributes["class"] = "headerQa"; 
     Page.Title = "FTP QA"; 
    } 
    else 
    { 
     siteHeader.InnerText = "FTP Forms - Production"; 
     siteHeader.Attributes["class"] = "headerPrd"; 
     Page.Title = "FTP Prod"; 
    } 
    base.Render(writer); 
}