【问题标题】:If inside html code with entity framework如果在带有实体框架的 html 代码中
【发布时间】:2013-12-27 19:22:25
【问题描述】:

我首先使用 asp.net webform 4.5.1 代码和实体框架。我使用了一个中继器并将其绑定到我的实体类。我想使用 if 语句来决定是否在这个中继器中显示一个 DIV。我的代码是:

<asp:Repeater ID="ProductRepeater" runat="server"
          ItemType="Models.Product"
          SelectMethod="ProductRepeate_GetData">
          <ItemTemplate>
             <% if(Item.Rank > 5 && Item.X != null && Item.Y != null){%>
               <div>I want show this div just if my if statement is True</div>
             <%}%>
             <div >
                 <%# Item.Name%>
              </div>

           </ItemTemplate>
</asp:Repeater>

我想在 if 语句的结果为 True 时显示第一个 div。错误是:当前上下文中不存在名称“项目”

【问题讨论】:

    标签: c# asp.net entity-framework webforms


    【解决方案1】:

    这不是您想要内联的那种计算;不仅阅读困难,调试也困难。

    相反,创建标签 &lt;asp:Label ID="outputLabel" runat="server" &gt;&lt;/asp:Label&gt; 并从转发器上的 ItemDataBound 事件中设置标签的值。

    protected void ProductRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
    
        RepeaterItem item = e.Item;
    
        Label output = (Label)item.FindControl("outputLabel");
        Product product = (Product)item.DataItem;
    
        if (product.Rank > 5 && product.X != null && product.Y != null)
        {
            output = "I want show this div just if my if statement is True";
        }
        else
        {
            output = product.Name;
        }
    }
    

    【讨论】:

    • 感谢您的回复。当我使用 Container.DataItem 而不是 Item。它显示了这个错误。当前上下文中不存在名称“容器”
    【解决方案2】:

    我知道你已经得到了答案,你也可以在标记中这样做:

     <%# (Item.Rank > 5 && Item.X != null && Item.Y != null)? "<div>I want show this div just if my if statement is True</div>" : "" %>
    

    【讨论】:

    • 我正在寻找这个答案。非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    相关资源
    最近更新 更多