gpt4 book ai didi

asp.net - 如何在页面中有条件地渲染 CSS

转载 作者:太空宇宙 更新时间:2023-11-04 14:32:45 26 4
gpt4 key购买 nike

我想根据 cookie 有条件地在页面中呈现 css。在我的服务器端,我检测到 cookie 并将 cookie 值存储在变量中,现在在 aspx 页面中,我想借助 if else 逻辑 根据存储在代码后面的变量中的 cookie 值呈现 css。

假设在我的 .cs 代码中我存储了像 strCountryCookie="GB" 这样的 cookie 值,在我的 aspx 页面中我尝试使用 if else 逻辑 渲染 css基于存储在变量中的 cookie 值。

所以这就是我正在尝试的方式。

<% 
if(strCountryCookie=="DE")
{ %>
#acloginpod {
width:380px;
background:#ebebeb url(../images/acloginpodbg.gif) repeat-x;
border:1px solid #d3d3d3;
-webkit-border-radius:7px;
-moz-border-radius:7px;
}
<% } else { %>

它显示错误。所以我无法弄清楚如何使用 if else 逻辑根据 cookie 值呈现它。所以请用概念指导我。谢谢

最佳答案

正如其他人所说,您不能在 CSS 中使用服务器端代码。如果您确保可以从后面的代码访问该字符串,那么您所做的几乎是正确的:

protected string strCountryCookie = "GB";

然后修正你的陈述

<head runat="server">
<title>Test</title>
<% if (strCountryCookie == "GB")
{ %>
<style type="text/css">
#acloginpod {
width:380px;
background:#ebebeb url(../images/acloginpodbg.gif) repeat-x;
border:1px solid #d3d3d3;
-webkit-border-radius:7px;
-moz-border-radius:7px;
}
</style>
<%} %>

虽然这会很快变得相当丑陋……尤其是如果您添加了一堆国家。

另一种选择是将所有自定义样式放入自己的样式表中,然后根据cookie 动态加载样式表。在这种情况下,您可以从缓存的样式表中获益:

<link id="_countryStyleSheet" rel="stylesheet" type="text/css" runat="server" />

然后在后面的代码中加载样式表:

_countryStyleSheet.Href = String.Format("~/styles/{0}.css", strCountryCookie);

在这个例子中,样式表将被命名为GB.css

关于asp.net - 如何在页面中有条件地渲染 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19023923/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com