C# 如果在带有实体框架的html代码中
我首先使用asp.net webform 4.5.1代码和实体框架。我使用了一个repeater并将其绑定到实体类。我想使用if语句来决定是否在此中继器中显示一个DIV。我的代码是:C# 如果在带有实体框架的html代码中,c#,asp.net,entity-framework,webforms,C#,Asp.net,Entity Framework,Webforms,我首先使用asp.net webform 4.5.1代码和实体框架。我使用了一个repeater并将其绑定到实体类。我想使用if语句来决定是否在此中继器中显示一个DIV。我的代码是: <asp:Repeater ID="ProductRepeater" runat="server" ItemType="Models.Product" SelectMethod="ProductRepeate_GetData"> <Ite
<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>
5&&Item.X!=null&&Item.Y!=空){%>
如果我的if语句为真,我想显示这个div
我想在if语句的结果为True时显示第一个div。错误是:当前上下文中不存在名称“Item”这不是您希望包含内联计算的类型;它不仅很难读取,而且也很难调试 相反,创建一个标签
,并通过中继器上的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;
}
}
我知道你已经得到了答案,你也可以这样做:
<%# (Item.Rank > 5 && Item.X != null && Item.Y != null)? "<div>I want show this div just if my if statement is True</div>" : "" %>
5&&Item.X!=null&&Item.Y!=null)?“只要我的if语句为True,我就想显示这个div”:“%>
感谢您的回复。当我使用Container.DataItem而不是Item时,显示了此错误。当前上下文中不存在名称“Container”。我正在搜索此答案。非常感谢您