在我的 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/