I'm getting “'Eval' is not declared. I

2019-08-27 15:21发布

问题:

I have an ascx that uses Eval in a repeater like this:

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="Similar.ascx.vb" Inherits="x.y.z.Similar" %>
<asp:repeater runat="server" id="rptAlternatives" visible="false" Enableviewstate="false">
    <HeaderTemplate>
        <section id="similar" class="sidelist sideModule">
            <h3>Possible Alternatives:</h3> 
            <ul class="sideborder">
    </HeaderTemplate>
    <ItemTemplate>
        <li>
            <a href="/bar/<%# Eval("URL")%>" class="box">foo</a>
        </li>
    </ItemTemplate>
    <FooterTemplate>
            </ul>
        </section>
    </FooterTemplate>
</asp:repeater>

This works fine on my development PC, but when I deploy it to our live server it fails saying: Compiler Error Message: BC30451: 'Eval' is not declared. It may be inaccessible due to its protection level. What's really odd is that sometimes restarting the application pool makes it go away and then sometimes it comes back again. Also it's not breaking on every page that uses Eval.

The local environment is IIS 7.5.7600.16385 with an application pool running ASP.NET v4.0 Classic. The server environment is the same. I publish from VS on my machine and copy this onto the live server to deploy.

Before this deployment the live site was using v2.0. Upgrading the site to v4.0 is part of the changes we're trying to release. This code has not changed as part of this release. It has always used Eval, and this has always been fine.

I have installed the Visual Studio 11 beta, but I'm using 2010 for this project.

Any ideas what could be causing this?

Edit:

This experts exchange post seems to describe exactly the same behaviour. They "fixed" it by changing the name of their class in both ascx and ascx.vb files. They matched before the change and they matched after the change, but the change obviously jogged something in the compiler. I might give this a go, but it doesn't seem satisfactory that a particular ascx would just stop working.

I've also found plenty of posts suggesting changing it to DataBinder.Eval(Container.DataItem, "URL"). However, I'm again reluctant to do this when the code worked before and has suddenly stopped. It's a magic and random "fix" that I'm unwilling to use.

回答1:

The important detail turned out to be that we were updating from v2.0 to v4.0 and that we were copying files over the top of the previous deployment.

There were v2.0 precompiled website DLLs still in the bin directory on the server from the previous deployment. As the precompiled DLL names change between releases the old ones were not replaced. Somehow having v2.0 and v4.0 DLLs in the same directory was confusing the server. As I didn't have these old DLLs on my development machine I was not seeing the same problems.

Deleting the old DLLs has fixed the issue.



回答2:

This happened in VS2017, in my case, when adding classes from other or older projects (Add->Existing Item) or when you add a Control/WebForm and than cut&paste code from other or older projects.

The workarond is as follows:

  1. Don't use Add->Existing Item
  2. Add a new EMPTY Control/WebForm (leave the default name)
  3. Open the code behind and RUN COMPILE on the empty default files
  4. Add some code in .aspx like a DIV tag
  5. Compile again
  6. Reference DIV tag from code behind file (i.e. DivId.Visible = False)
  7. Compile again
  8. Now the assembly/compiler is hooked to the new file/class
  9. Now cut&paste the code from the other projects to the editor
    1. Compile again
    2. NO MORE ERRORS!!

Enjoy!