1

I'm using the Repeater control. All the property names in my data source are represented in my ImportColumns enum.

So I am trying to loop through the enums to pass the names to the Eval method.

This is in ItemTemplate section of my Repeater control.

Line 57:  <tr class="ProductRow" data-id="<%# Eval("Id") %>">
Line 58:      <% foreach (var column in (ImportColumns[])Enum.GetValues(typeof(ImportColumns))) { %>
Line 59:          <td><%# Eval(column.ToString()) %></td>
Line 60:      <% } %>
Line 61:  </tr>

But line 59 gives me the following error.

CS0103: The name 'column' does not exist in the current context

Why doesn't the code recognize the column variable? I'm guessing it either has something to do with the fact that I'm in a repeater control or that I'm missing <% %> code and <%# %>, but it seems like this should work.

EDIT:

I see that if I change line 59.

<td><%= column.ToString() %></td>

There is no error. (Although it no longer does what I need.)

So this has something to do with Eval that prevents "regular" variables from working. Does anyone know of a workaround to this?

2
  • I believe that the error at 59 is because the Eval wants to work on the object that is databound to the Repeater. The Eval in line 57 works because ID is a property of whatever object you are bound to. column.ToString() is not one of the properties of that class. When you say your edit no longer does what you need, what does it do? Was it just an integer value? Commented May 28, 2014 at 22:10
  • I know that Eval works on the databound object, but it takes a string argument as the name of the property on that object. I am not changing the object being evaluated. I'm only changing where the string argument comes from. I don't see where I said my edit no longer does what I need. Commented May 28, 2014 at 22:24

2 Answers 2

1

The column variable is not recognized because <%# ... %> data-binding expressions are evaluated when the container is data-bound, whereas <% ... %> and <%= ... %> code blocks are executed when the page is rendered. If you click "Show Complete Compilation Source" on the error page, you'll see that

foreach (var column in (ImportColumns[])Enum.GetValues(typeof(ImportColumns))) {

and

Eval(column.ToString())

are located in two separate methods.

One possible solution is to combine the loop and Eval into a single data-binding expression. For example:

<%#
    string.Concat(Enum.GetValues(typeof(ImportColumns)).Cast<ImportColumns>()
        .Select(column => "<td>" + Eval(column.ToString()) + "</td>")) 
%>
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. As I suggested in my question, I knew it has something to do with the <%#...%> syntax. But I never seemed to have gained a very deep understanding of what those are doing under the covers.
And, FYI, I resolved this by dumping the Repeater control and just using a <% foreach %> loop.
One more question for you, please. Where is this "Show Complete Compilation Source" link you mentioned. I don't see it on the error page.
@Jonathan: When I try your original code, I get the standard ASP.NET "Server Error" page which displays "Description", "Compiler Error Message", "Source Error" (with source code excerpt in yellow), and "Source File". Below that are two links, "Show Detailed Compiler Output" and "Show Complete Compilation Source".
Thanks, I'll have to experiment with that. Appears it shows up for just some errors.
0

I think what you are typing to do is use the name of the enumeration entries to be used as the property name for the Eval

If so then use the Enum.GetNames instead of the GetValues method.

Line 57:  <tr class="ProductRow" data-id="<%# Eval("Id") %>">
Line 58:      <% foreach (string column in Enum.GetNames(typeof(ImportColumns))) { %>
Line 59:          <td><%# Eval(column) %></td>
Line 60:      <% } %>
Line 61:  </tr>

Edit:

You are right I didn’t test that. I also don’t use inline code so I don’t understand the intricacies. Here is a solution that works for me.

I would put that loop logic in a method in the code behind file that creates the complete row and then invoke it from the aspx page.

In the aspx

<tr class="ProductRow" data-id="<%# Eval("Id") %>">
    <%# this.ColumnDetail() %>
</tr>

The method in the code behind

protected string ColumnDetail() {
    string html = String.Empty;
    foreach (string x in Enum.GetNames(typeof(ImportColumns))) {
        html = (html + string.Format("<td>{0}</td>", Eval(x)));
    }
    return html;
}

3 Comments

No, the error is that the variable is simply not recognized. And Eval takes a string so column.ToString() should work fine. But that block of code knows nothing about the variable.
Are you trying to use the enumeration item names as the property names for the data bound object?
Yes, of course. And the name is exactly what you get with column.ToString(). It may make better sense to use Enum.GetNames(), I haven't looked at that. But that has nothing to do with the issue I'm having and I can see you didn't test it because it simply doesn't recognize the loop variable. How I use that variable doesn't seem material at this point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.