Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 270
user244269 Avatar asked Jun 04 '14 05:06

user244269


3 Answers

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

like image 144
Merenzo Avatar answered Oct 22 '22 06:10

Merenzo


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

   <%#(Eval("IsBreakPoint")) == "1" ? Eval("Category", "<tr bgcolor:#D4FFC4><td colspan='28'><b>{0}</b></td></tr>") : ""%>
like image 45
user244269 Avatar answered Oct 22 '22 05:10

user244269


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

like image 31
Bishoy Hanna Avatar answered Oct 22 '22 06:10

Bishoy Hanna