【问题标题】:Using XPath With Conditional Statement Inside Repeater在中继器中使用 XPath 和条件语句
【发布时间】:2012-12-06 04:10:17
【问题描述】:

我正在尝试向我的转发器添加条件语句,但收到以下错误:

  • Eval()、XPath() 和 Bind() 等数据绑定方法只能在数据绑定控件的上下文中使用。

我尝试使用的代码如下:

<asp:Repeater ID="repLeftmenu" DataSourceID="xmlMenu" runat="server" EnableViewState="false">
  <HeaderTemplate>
    <ul class="leftMenu">
  </HeaderTemplate>
  <ItemTemplate>
    <li>
      <%  If XPath("@url").ToString = HttpContext.Current.Request.Url.AbsolutePath.ToString Then%>
        <a href="<%# XPath("@url") %>">
          <%# XPath("@title")%>
        </a>  
      <% Else %>
        <a href="<%# XPath("@url") %>">
          <%# XPath("@title")%>
        </a>              
      <%  End If%>
   </li>
 </ItemTemplate>
 <FooterTemplate>
   </ul>
 </FooterTemplate>
</asp:Repeater>    

我的语法有问题吗?

谢谢, 垃圾

【问题讨论】:

  • 查看您的代码,我看到了一个 if/else 构造,但我发现 If Then ... Else 中包含的代码与 Else ... End If 中的代码之间没有区别。您确定需要 if/else 构造吗?

标签: asp.net vb.net


【解决方案1】:

您在常规代码块中使用数据绑定表达式,这是不受支持的。数据绑定表达式应该出现在&lt;%# ... %&gt; 块内。常规代码块如下所示:&lt;% ... %&gt;。 (唯一的区别是块开头的#。)

如果您需要在数据绑定块内使用条件,则需要使用表达式(简单地说:单行)。完成此操作的唯一方法是使用以下代码:

<%# If(XPath("Url").ToString = HttpContext.Current.Request.Url.AbsolutePath.ToString, "Display this when true", "Display this when false") %>

当有条件地显示整个 HTML 块时,这很快就会变得笨拙。然后你可以做的是创建两个块并根据条件表达式设置每个块的可见性:

<span runat="server" visible='<%# XPath("Url").ToString = <%# If(XPath("Url").ToString = HttpContext.Current.Request.Url.AbsolutePath.ToString %>'>

  HTML that should be displayed when condition is true

</span>
<span runat="server" visible='<%# XPath("Url").ToString <> <%# If(XPath("Url").ToString = HttpContext.Current.Request.Url.AbsolutePath.ToString %>'>

  HTML that should be displayed when condition is false

</span>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-02
    • 2017-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    相关资源
    最近更新 更多