3

For whatever reason my .aspx will only recognize the code behind when the Page directive is using CodeFile and not CodeBehind. Also, it gives a parser error on runtime. But I need CodeBehind to publish the site.

  • Every other page in the web application uses CodeBehind.
  • I've tried deleting and recreating the file, even leaving it empty, still an error.
  • I've tried deleting the designer file and converting the .aspx to a web application.
  • The page directive references (namespace, class) are correct.
  • Tried deleting the obj.
  • Tried cleaning and rebuilding.

I'm out options here. Everything I've googled so far hasn't panned out. Why can't CodeBehind be used?!

.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/App/Masters/Default.Master" AutoEventWireup="true" CodeBehind="BlogPost.aspx.cs" Inherits="WebApplication.App.Templates.BlogPost" %>

.aspx.cs

namespace WebApplication.App.Templates
{
    public partial class BlogPost : BaseTemplate
    {
TylerH
  • 20,799
  • 66
  • 75
  • 101
Ber53rker
  • 1,036
  • 3
  • 17
  • 26
  • 1
    http://stackoverflow.com/questions/73022/codefile-vs-codebehind - maybe you're using the wrong attribute? – Tim Dec 06 '13 at 20:59
  • @Tim CodeFile works, but won't work when published. Also, every other page works with CodeBehind fine. It's just this one all of a sudden. – Ber53rker Dec 06 '13 at 21:00
  • 1
    I want to say I saw this before - it's one specific page, right? I think when it happened to me I did a clean/rebuild and it worked. Something else you might want to try is deleting the IIS Temporary Internet Files. You've got to stop IIS and manually drill down to the directory. (I used to see this a lot when I was doing work with the Ektron CMS) – Tim Dec 06 '13 at 21:05

1 Answers1

4

Maybe your .csproj file was corrupted somehow?

Check to see that the entry for your file(s) look like this in your project file:

<Compile Include="BlogPost.aspx.cs">
  <DependentUpon>BlogPost.aspx</DependentUpon>
  <SubType>ASPXCodeBehind</SubType>
</Compile>
<Compile Include="BlogPost.aspx.designer.cs">
  <DependentUpon>BlogPost.aspx</DependentUpon>
</Compile>
Jesse Webb
  • 43,135
  • 27
  • 106
  • 143
  • This looks like it might be it. I can't find them. Should I manually add them or is there a better way? – Ber53rker Dec 06 '13 at 21:06
  • @Ber53rker My suggestion would be to just re-create the file and it should be added to the `.csproj` file automatically. Although you said "I've tried deleting and recreating the file" so I am not sure. Either way, you can edit the csproj files manually with no worries, just make sure you "Save All" in Visual Studio first. – Jesse Webb Dec 06 '13 at 21:08
  • @Ber53rker Also, make sure that when you re-create the file, you are using the correct template. Use `C# > Web > Web Form`. – Jesse Webb Dec 06 '13 at 21:16
  • This worked. Also had to add a reference to the .aspx. I'm concerned about why this is happening still. Haven't found anything Google-wise about things not being automatically added to the .csproj. Hopefully when I commit this won't cause my colleagues issues. – Ber53rker Dec 09 '13 at 17:38
  • Look here. http://stackoverflow.com/questions/36202205/master-page-wont-compile-when-referencing-its-own-properties-and-members/36202409#36202409 Sometimes items can be out of order in the .csproj file. – O. Jones Apr 12 '16 at 16:25