9

I am getting the following error

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

but I am trying to write my code in ASP.NET REPEATER Control as

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

Please help

Gwenc37
  • 2,064
  • 7
  • 18
  • 22
user244269
  • 111
  • 1
  • 1
  • 3
  • possible duplicate of [Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control](http://stackoverflow.com/questions/2571545/databinding-methods-such-as-eval-xpath-and-bind-can-only-be-used-in-the) – JNF Jan 13 '15 at 16:25

4 Answers4

15

The <% if %> statement doesn't support databinding.

For conditional display, I always try to databind to the Visible property of a single server control.

In a case like yours involving a block of markup (rather than a single server control), I'd wrap that block in an <asp:PlaceHolder> control as follows:

<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>

Or if you aren't really using that label server-side:

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

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

</asp:PlaceHolder>

Or more readable still: if you can define the ItemType property for the Repeater, then you will get strong typing and Intellisense at design-time (this would be my recommended approach):

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

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

</asp:PlaceHolder>

Take care to use single quotes around the Visible value when that expression contains double quotes. (Ahh, as you have already done with the Label.Text property.)

Merenzo
  • 5,326
  • 4
  • 31
  • 46
2

Got an answer... and it worked...

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

try to change that to <%#DataBinder.Eval(Container.DataItem,"field")%>

Bishoy Hanna
  • 4,539
  • 1
  • 29
  • 31
0

We Can not Use Conditional Method Like if .. Else switch ... Case in DataList ...

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

use

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