c# - if 语句中的数据绑定(bind)

标签 c# asp.net data-binding

在我的 aspx 页面中:

<asp:HyperLink ID="HyperLink" runat="server" style="cursor:pointer; text-decoration:none;" NavigateUrl='<%#String.Format("~/storefront.aspx?CatalogID={0}",Eval("CatalogID"))%>'>
<asp:Label id="lblCustItem" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CustItem")%>' width="15%">
</asp:Label>
</asp:HyperLink>

现在我正在尝试做:

<%if (Eval("Integration").ToString() == "Y")
{ %>
<asp:HyperLink ID="HyperLink1" runat="server" style="cursor:pointer; text-decoration:none;" NavigateUrl='<%#String.Format("~/integration/vendorframe.aspx?CatalogID={0}",Eval("CatalogID"))%>'>
<asp:Label id="CustItemlbl" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CustItem")%>' width="15%">
</asp:Label>
</asp:HyperLink>
<%} %>

<%else
{ %>
<asp:HyperLink ID="HyperLink" runat="server" style="cursor:pointer; text-decoration:none;" NavigateUrl='<%#String.Format("~/storefront.aspx?CatalogID={0}",Eval("CatalogID"))%>'>
<asp:Label id="lblCustItem" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CustItem")%>' width="15%">
</asp:Label>
</asp:HyperLink>
<%} %>

第二段代码出现页面错误。所以我的问题是,我做错了什么吗,有没有更好的方法来使用 if 语句,比如条件 if,但我确实需要运行一个新的 string.format 实例,这就是为什么我认为那不是一个选项。

错误信息: Eval()、XPath() 和 Bind() 等数据绑定(bind)方法只能在数据绑定(bind)控件的上下文中使用。

最佳答案

一种解决方案是使用内联 If:

Text='<%# (Eval("Integration").ToString() == "Y") ? DataBinder.Eval(Container.DataItem, "CustItem") : "" %>'

它不是很漂亮,但它可以完成工作。

关于c# - if 语句中的数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7570287/

相关文章:

c# - WPF MVVM 和具有依赖属性的嵌套 View

javascript - Angular float 向下小数

c# - 为什么 IEnumerable<T> 需要调用 ToList 来更新 ListView ?

c# - 从字符串中获取完全限定的类型名称.Net C#

c# - 如何在同一站点的 ashx 和 aspx 文件中使用 await/async?

javascript - Web 应用程序中的时区敏感日期和时间显示?

asp.net - 在asp.net中显示按月和年分组的记录

c# - Windows 8 网格应用程序中的自定义详细信息页面

具有 InternalCall 属性的 C# 内部静态外部 - 内部还是外部?

c# - 动态 linq : Is there a way to access object data by index?