用HtmlLink来改变网站的主题

时间:2024-07-31 17:36:02
#region Theme
// 注册样式(将主题样式至于通用样式后面)
HtmlLink themeCss = new HtmlLink();
themeCss.Href = GetThemeUrl();
themeCss.Attributes["id"] = "commonPageStyle";
themeCss.Attributes["rel"] = "stylesheet";
themeCss.Attributes["type"] = "text/css";
int commonCssIndex = 0;
bool existsCommonCss = false; foreach (Control control in Page.Header.Controls)
{
if (control is HtmlLink && (control as HtmlLink).Href.IndexOf("/css/commPage.css", StringComparison.OrdinalIgnoreCase) > -1)
{
Page.Header.Controls.AddAt(commonCssIndex + 1, themeCss);
existsCommonCss = true;
break;
}
commonCssIndex++;
}
if (!existsCommonCss)
{
Page.Header.Controls.AddAt(0, themeCss);
}
#endregion
private string GetThemeUrl()
{
string themeStr = "/css/themes/{0}/commPage.css?v=140917";
switch (UserSession.getInstance().UserTheme)
{
case "blue":
themeStr = string.Format(themeStr, "blue");
break;
case "purple":
themeStr = string.Format(themeStr, "purple");
break;
case "red":
themeStr = string.Format(themeStr, "red");
break;
case "green":
themeStr = string.Format(themeStr, "green");
break;
default:
themeStr = string.Format(themeStr, "default");
break;
}
return themeStr;
}