在Umbraco中进行简单的C#检查

时间:2014-10-20 17:31:27

标签: c# umbraco

我想检查一下是否是一个整数,所以如果它是8,那么为某个变量分配一个特定的值,如果不是,另一个变量,非常简单,但我得到一个错误?

var getColor = umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("textColor" + i).Value;
                if (getColor == 8)
                {
                    var color = "black";
                }
                else
                {
                    var color = "white";
                } 

然后我想把我的HTML中的变量打印成类似的类:

<div class="headline <% color %>"></div>

整个代码如下:

<div class="slides" onmouseleave="startSlider();">
            <%for(int i = 1; i < 5; i++) { %>
            <%

                var getColor = umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("textColor" + i).Value;
                if (getColor == "8")
                {
                    var color = "black";
                }
                else
                {
                    var color = "white";
                } 
            %>
                <%if(umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("image"+ i).Value != "") {%>
                    <div class="slide" id="slide<%=i%>" onclick="location.href = '<%=umbraco.library.NiceUrl(int.Parse(umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("link"+ i).Value))%>';" style="display:<%if(i != 1){%>none<%}%>;" tmpcolor="<%=umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("backgroundColor"+ i).Value%>">
                    <div class="image" id="image<%=i%>" style="background: url(<%=umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("image"+ i).Value%>) center center;"></div>
                    <div class="headline <%=color%>"><%=umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("headline"+ i).Value%></div>
                    <div class="subline"><%=umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("subline"+ i).Value%></div>
                    <div class="menuitems">
                        <%for(int y = 1; y < 5; y++) { %>
                            <%if(umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("menutext"+ y).Value != "") {%><span class="item<%if(i==y){%> selected<%}%>" onmouseover="gotoSlide(<%=y%>);"><a title="<%=umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("headline"+ i).Value%>" href='<%=umbraco.library.NiceUrl(int.Parse(umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("link"+ y).Value))%>'><%=umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("menutext"+ y).Value%></a><br/></span><%}%>
                        <%}%>
                    </div>
                </div>
                <%}%>
            <%}%>
        </div>

我得到错误&#34;颜色不在当前上下文&#34;

1 个答案:

答案 0 :(得分:0)

Value返回一个字符串,该字符串取决于您用于文档类型属性的数据类型。因此,你不能说“getColor == 8”。

以下是我在.master或.aspx页面上的测试环境中工作的一些片段。

<script runat="server">
    string GetColorClass()
    {
        var color = umbraco.NodeFactory.Node.GetCurrent().GetProperty("textColor1").Value;
        color = color == null ? string.Empty : color;
        switch (color)
        {
            case "8":
                return "black";
            default:
                return "white";
        }
    }
</script>

<div class="<% =GetColorClass() %>">
    <h3>Cool It Works!</h3>
</div>

现在,还要注意我正在使用&lt;%= ...而不是你所拥有的&lt;%。

&lt; %%&gt; =内联代码(不在响应中插入任何内容,只执行内联代码) &lt;%$%&gt; =用于使用web.config中注册的表达式构建器之一来计算表达式 ...

&lt;%=%&gt; :response.write的简写(这是你想要的,因为它会将输出插入到遇到的响应中,例如类似于class =“attribute。

有关这些内容的更完整列表,请参阅我从<%$, <%@, <%=, <%# ... what's the deal?

发布的帖子

我还使用了来自不同命名空间的节点工厂,您从中访问它的命名空间已被标记为已过时,可以从将来的版本中删除。