views:

1371

answers:

6

I have the following definition at the top of my .ASCX file:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ArticleView.aspx.cs" Inherits="MyNameSpace.ArticleView" %>

In that control I make use of <%= %> blocks to refer to members that I've declared in the code-behind file. If I compile and deploy the control, it works fine. But in Visual Studio I get a lot of design-time errors, "{some variable} does not exist in the current context." And Intellisense breaks too: it works for members of UserControl, but can't find my own declared members. There are other issues as well. In general, everything points to the fact that the ASP.articleview_ascx class getting generated is somehow not inheriting from the MyNameSpace.ArticleView class.

I've found that if I switch the CodeBehind attribute to "CodeFile":

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ArticleView.aspx.cs" Inherits="MyNameSpace.ArticleView" %>

suddenly Intellisense works and all the design-time errors disappear. But I don't want to do runtime compilation, or deploy my .ASCX.CS files - so I can't use CodeFile.

I've checked the simple stuff, like making sure that my CodeBehind filename is correct & the Inherits class has the proper namespace, etc. (And since it works properly after changing the attribute to CodeFile, those must be pointing at the right place....) But what am I missing? Why can't it handle the CodeBehind attribute?

Thanks,
Steve


Update: from a thread below - basic question was, why not just use CodeFile? Answer: when I try to deploy using CodeFile= in my files, after deploying I receive the following stack trace (presented in its entirety):

/_layouts/Pages/ViewPage.aspx.cs' does not exist. at System.Web.UI.Util.CheckVirtualFileExists(VirtualPath virtualPath) at System.Web.UI.TemplateParser.ProcessCodeFile(VirtualPath codeFileVirtualPath) at System.Web.UI.TemplateParser.ProcessMainDirectiveAttribute(String deviceName, String name, String value, IDictionary parseData)

(This is from a request to /_layouts/Pages/ViewPage.aspx. ViewPage is the page that has several other controls including the ArticleView mentioned in my original example. It just happens to be the first file that fails - if I go back to CodeBehind= in ViewPage, then included ASCX with CodeFile= will fail in the same way.) This seems to be the page compiler complaining because the inherited codebehind class can't be found in any loaded DLL, so it expects there must be a CS file to do on-demand compilation.

The issue here is that I don't want to deploy CS files, just ASPX/ASCX. Having read through many articles like this great one I'm aware of the various new models of deployment, although I've never used anything but a Web Application Project (converted forward from VS2003, we were late adopters of 2005 & the WAP model had already been added by the time we switched up from 2003.) Over many VS2005/8 projects, I've never had a problem with CodeBehind=, until this Intellisense issue showed up... though it doesn't help that in this case I'm deploying to SharePoint, which introduces a whole new level of complexity.

Since I've not deployed using CodeFile before, it's very likely that I'm missing some option I'm supposed to set in VS when building, in order to force a pre-compile. I just need to be able to deploy, as I do today, as a set of ASPX/ASCX with a single codebehind DLL. And that's working today with CodeBehind= ... it just has the originally mentioned Intellisense problem, which is really what I want to fix :)

Will post more as I identify what files might be relevant to the question...

+2  A: 

CodeBehind is deprecated in .NET 2.0. I believe that only <= 1.1 uses "CodeBehind". Now it is "CodeFile" as you say.

Why do you not want to compile your code? If you compile you don't have to deploy your .cs files...

Jason
Let me clarify - as far as I understand, CodeBehind is still supported, but CodeFile is preferred. In any case, we do compile our code, and in fact that's why I want to use CodeBehind rather than CodeFile, which seems to require me to deploy the .ascx.cs as well as the .ascx file. All we deploy now (and all we want to continue deploying) is the .ascx file, with the codebehind info coming from 1 shared Assembly. Am I misunderstanding CodeFile?
Steve Eisner
have you tested deployment without your .cs files? if you are running a compiled project you never have to deploy your code files, just your .aspx. Fix, rebuild, redeploy, done.
Jason
Thanks, Jason. I did test it briefly and the environment (a Sharepoint site) reported that the page couldn't load because the CS file was missing. But I might just be missing some sort of compilation option, so I'll look into that! What you've described is exactly what I want to do... and to do it with CodeFile if possible is great.
Steve Eisner
make sure you've included the .cs file and are referencing it in your .aspx file. maybe try rebuilding your solution a couple times and then attempt to deploy again. it should work, i'm telling you. ha.
Jason
Yeah, I'm still getting: The file '/_layouts/Pages/ViewPage.aspx.cs' does not exist. at System.Web.UI.Util.CheckVirtualFileExists(VirtualPath virtualPath) at System.Web.UI.TemplateParser.ProcessCodeFile(VirtualPath codeFileVirtualPath) at System.Web.UI.TemplateParser.ProcessMainDirectiveAttribute(String deviceName, String name, String value, IDictionary parseData) ... this is the ASPX compiler telling me that for whatever reason, it needs the CS file to be deployed. Man, I hate MS right now. I've spent over a day trying to decipher this :(
Steve Eisner
hate to break it to you, but this isn't MS's fault :\ it sounds like you aren't referencing the file correctly...
Jason
Steve Eisner
Meanwhile, thanks for your help. I'll look for info on using compiled ASPX files in Sharepoint separately. The original question still stands, though :(
Steve Eisner
you should post your whole error message including your call stack here. believe me, this is not MS's fault... without trying to be mean, this is definitely a problem that YOU created, as much as you don't want to believe it. MS is providing you with an error message that is trying to tell you what is wrong. I'm sure once you figure it out, you'll facepalm yourself. In the meantime, post your whole error message in your question.
Jason
Sorry, yeah, that literally is the entire error message I'm being given (previous comment re: /_layouts/Pages/....) The failure happens in SharePoint, which is not known for verbose error messages :( I'll see if I can collect everything into a useful debugging scenario and post an update. What's throwing me is that the deployed code works 100% with CodeBehind=, but Intellisense fails during design. When I switch the single attribute to CodeFile=, Intellisense now works but the deployment fails with stack above (.CS files missing.) Seems to indicate that namespaces/filenames/etc are fine?
Steve Eisner
BTW at this point I'd be very happy to facepalm - I'm pragmatic :) so whatever end is reached is fine by me! Will try to continue this with more info added to the original question thanks...
Steve Eisner
you know what i would do at this point, i would just copy all your relevant code from both pages and paste into notepad or something, delete your files, create fresh ones, and paste your code back in. recompile. done.
Jason
Yeah, I've read a suggestion that maybe I should re-create the whole project, too - in case some WAP settings are broken. Hoping not to have to do that (it's a very large project) so I've put out a bounty in case anyone knows how to manually fix this. But I'm happy to pay that to you if your suggestion works!
Steve Eisner
i wouldn't go so far as to say you need to recreate the project... i'm just talking about the file(s) you're having problems with. it could be something as stupid as the designer files haven't updated for some reason or another. i've had that happen before. let me know if it works! good luck
Jason
CodeBehind is not deprecated, I'm looking at a Web Application user control right now in Visual Studio 2010 Beta 1 with CodeBehind used in it by default. I didn't put it there. It's only for Web Site projects that the default is CodeFile. I always found source control hell with Web Site projects, I believe that's why they added back support for WAP as an addin project template back in whatever version it was.
Richard Hein
That's also the reason that we're continuing to use CodeBehind - none of the "new" project formats are useful; they only create extra burdens, whether at the source control level or deployment of hundreds of DLLs :( Luckily, we didn't move to VS2005 until they'd introduced the WAP templates!
Steve Eisner
+4  A: 

This has happened to me before. Try right clicking the ascx/aspx and click on "Convert to Web Application". You may just be missing the generated controls. If you don't see it in the context menu, delete the designer generated file first.

CitizenBane
+4  A: 

You are missing the [your-file].ascx.designer.cs file, which links your controls to your codebehind.

Just like CitizenBane suggestions, you need to right-click the file (or folders, or entire web project) and select "Convert to Application". Visual Studio will examine your ascx/aspx files for the server controls, and generate that designer file for you.

I actually ran into this myself, on a far larger scale... http://stackoverflow.com/questions/753943/c-how-to-convert-a-website-project-to-a-web-project

Check the answer.

eduncan911
+1 for explaining it better than I did!
CitizenBane
Heh. And I am the one who voted your answer up.
eduncan911
Thanks - this is a common explanation on the web ("Convert to Web Application") but I had found that and it didn't fix it. The designer files do exist, and references to controls named in the ASCX file do work from the .ASPX.CS file. However, I think you're on to something, and my project is probably not "well-formed". Jason also suspected something similar... I'm looking into it to see if I can re-create the files or figure out what's not hooked up properly!
Steve Eisner
What I found in the link I supplied above was that my code behind files were not namespaced properly. The "Convert to Web" resolved it for me, but if you manually have been trying to fix it - that will cause VS to loose the auto-generation of the designer requirements perhaps.
eduncan911
+2  A: 

Why do you have the code behind for your ascx control as an aspx named page code behind? A UserControl (ascx) usually has a codebehind of

CodeBehind="ArticleView.ascx.cs"

instead of what you have listed

CodeBehind="ArticleView.aspx.cs"

Notice the aspx instead of the ascx for a User Control.

That could be your problem... a simple typo or a copy and paste error. Couple possibilities come to mind:

  1. Maybe you have the ascx control (User Control) specified above using a code behind file that is inheriting from System.Web.UI.Page instead of System.Web.UI.UserControl (that could be causing the Visual Studio errors).
  2. You have the UserControl pointed at the code behind for a same name aspx page. Similar problem as #1 which would cause Visual Studio to get all confused.
  3. Your files are name ArticleView.ascx and ArticleView.aspx.cs. This might confuse Visual Studio since I believe VS might expects a particular naming convention.

For a User Control (ascx) your files should be named:

  • ArticleView.ascx (CodeBehind="ArticleView.ascx.cs" Inherits="[NAMESPACE].ArticleView")
  • ArticleView.ascx.cs (inherits from System.Web.UI.UserControl)
  • ArticleView.ascx.designer.cs

For a Web From (aspx) your files should be named:

  • ArticlePage.aspx (CodeBehind="ArticlePage.aspx.cs" Inherits="[NAMESPACE].ArticlePage")
  • ArticlePage.aspx.cs (inherits from System.Web.UI.Page)
  • ArticlePage.aspx.designer.cs
Jeff Widmer
+1 for the good eye, but it might be a typo and not even the real control's name etc....
Richard Hein
Thanks, you're totally right about the typo... but it's a typo that happened as I was preparing a smaller example case to post to Stack Overflow. I was trying to clean things up and accidentally typed "ASPX". The controls and forms are named as you've explained. Great catch though!
Steve Eisner
+3  A: 

Have you checked the Build Action on your project files? I have duplicated your issue by setting the Build Action on ArticleView.ascx.designer.cs to None. I can also compile when using CodeFile, etc..., I'm 99% sure that's your problem.

Richard Hein
Wow, that was it. Others were very close to the cause - the designer.cs files must be out of sync in some way - but this was the exact reason. I don't know how it got that way, but the designer files for all the classes I had created over the last two weeks were set to Build = None. I can understand why CodeFile would solve this but I don't yet understand why the project, when deployed, was working at all :) I guess the designer files were being generated on demand? Oh well
Steve Eisner
It's tricky to duplicate because you can't build like that, but the page will load and yes, ASP.NET is creating the declaration that is missing when the page is loaded. That's the ASP.NET pipeline in action. Working on Sharepoint probably made this less apparent as the cause because debugging and deployment is so ... different to put it kindly.
Richard Hein
Thanks for the bounty reward. :)
Richard Hein
Thanks Richard! I had not thought of this.
CitizenBane
Nice one Richard. I had a similiar problem (no Intellisense in .cs file) and changed the 'Build Action' property of my class file to 'Compile' and then it worked perfect. Cheers for the tip.
David HAust
A: 

Hi Scott,

I am facing problem in VS 08, after installing VS sp1 or MVC-2. In my not MVC project, In code file, Intelisence not working, if I declare controls in code file it compiles but generates run-time error that controls alreadys exist, if I remove declaration it does'nt complite.

Please suggest a solutions

Regards

Mumtaz Ali

Mumtaz Ali