0

With a set up like this:

<asp:ListView runat="server" ID="lvArticles">
    <LayoutTemplate>
        <div runat="server" id="itemPlaceholder" />
    </LayoutTemplate>
    <ItemTemplate>
        <% if (Eval("Document") != null) { %>
            <a href="/Documents/" + <%# Eval("Document.Id") %> + ".pdf">
                <%# Eval("Document.Name") %>
            </a>
        <% } %>
    </ItemTemplate>
</asp:ListView>

I get the following error:

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

Why is databinding methods like Eval() not allowed in if conditions like that? Is the ListView not a databound control?

Joakim Johansson
  • 3,196
  • 1
  • 27
  • 43
  • 1
    Have you seen this post? http://stackoverflow.com/questions/2571545/databinding-methods-such-as-eval-xpath-and-bind-can-only-be-used-in-the – Ben Strombeck Aug 12 '13 at 15:41
  • @BenStrombeck I have, and it shows alternate ways of doing it, but doesn't answer the question. There are several of them. – Joakim Johansson Aug 12 '13 at 15:43
  • 1
    Why would you want to do this mess in the first place? That should be the real question! – banging Aug 12 '13 at 18:19
  • @banging Because I'm fed up with having to use Controls and code behind for tasks that shouldn't need them, and the soup of Events that follow. I'd switch to MVC but for the project I'm working on that's a bit too much to fix right now. In my opinion, having a PlaceHolder or Panel that gets Visible toggled, and then having to check for null values in the Eval anyway, well, it feels even worse than this mess. – Joakim Johansson Aug 12 '13 at 19:15

1 Answers1

1

Because the # inside <%# Eval("value") %> is what tells the server to do the data binding.

guest
  • 26
  • 1