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
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.)
Got an answer... and it worked...
<%#(Eval("IsBreakPoint")) == "1" ? Eval("Category", "<tr bgcolor:#D4FFC4><td colspan='28'><b>{0}</b></td></tr>") : ""%>
try to change that to <%#DataBinder.Eval(Container.DataItem,"field")%>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With