【问题标题】:Error: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control in repeater control错误:Eval()、XPath() 和 Bind() 等数据绑定方法只能在转发器控件中的数据绑定控件的上下文中使用
【发布时间】:2014-06-04 05:40:21
【问题描述】:

我收到以下错误

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control

但我正在尝试在 ASP.NET REPEATER 控件中编写我的代码

<%if (Eval("IsBreakPoint") == "1")
    { %>
        <tr>
            <td>
                <asp:Label ID="lblCategory" runat="server" Text='<%#Eval("Category") %>'></asp:Label>
            </td>
            <td colspan="27">
            </td>
       </tr>
    <%} %>

请帮忙

【问题讨论】:

标签: asp.net eval repeater


【解决方案1】:

&lt;% if %&gt; 语句不支持数据绑定。

对于条件显示,我总是尝试将数据绑定到单个服务器控件的Visible 属性。

在像您这样涉及标记块(而不是单个服务器控件)的情况下,我会将该块包装在 &lt;asp:PlaceHolder&gt; 控件中,如下所示:

<asp:PlaceHolder ID="CategoryPlaceHolder" runat="server" Visible='<%# Eval("IsBreakPoint") == "1") %>'>

    <tr>
        <td>
            <asp:Label ID="lblCategory" runat="server" Text='<%#Eval("Category") %>'></asp:Label>
        </td>
        <td colspan="27">
        </td>
   </tr>

</asp:PlaceHolder>

或者,如果您没有真正在服务器端使用该标签:

<asp:PlaceHolder ID="CategoryPlaceHolder" runat="server" Visible='<%# Eval("IsBreakPoint") == "1") %>'>

    <tr>
        <td>
            <%# Eval("Category") %>
        </td>
        <td colspan="27">
        </td>
   </tr>

</asp:PlaceHolder>

或者更具可读性:如果您可以为中继器定义ItemType 属性,那么您将在设计时获得强类型和智能感知(这是我推荐的方法):

<asp:PlaceHolder ID="CategoryPlaceHolder" runat="server" Visible='<%# Item.IsBreakPoint == "1") %>'>

    <tr>
        <td>
            <%# Item.Category %>
        </td>
        <td colspan="27">
        </td>
   </tr>

</asp:PlaceHolder>

当表达式包含双引号时,请注意在 Visible 值周围使用单引号。 (啊,正如您已经对 Label.Text 属性所做的那样。)

【讨论】:

    【解决方案2】:

    得到了答案...并且成功了...

       <%#(Eval("IsBreakPoint")) == "1" ? Eval("Category", "<tr bgcolor:#D4FFC4><td colspan='28'><b>{0}</b></td></tr>") : ""%>
    

    【讨论】:

      【解决方案3】:

      尝试将其更改为&lt;%#DataBinder.Eval(Container.DataItem,"field")%&gt;

      【讨论】:

      • 我喜欢这个解决方案,简单实用
      【解决方案4】:

      我们不能使用条件方法,例如 if .. Else switch ... Case in 数据列表 ...

      <%if (Eval("IsBreakPoint") == "1")
      

      使用

      <tr>
              <td>
                  <asp:Label ID="lblCategory" runat="server" Text='<%#Eval("Category") %>' visible="<# Eval("IsBreakPoint")==1 "></asp:Label>
              </td>
              <td colspan="27">
              </td>
         </tr>
      

      【讨论】:

        猜你喜欢
        • 2018-06-21
        • 2012-12-10
        • 2011-02-04
        • 2012-05-02
        • 2014-05-24
        • 1970-01-01
        • 2011-02-15
        • 1970-01-01
        • 2017-01-05
        相关资源
        最近更新 更多