Master page won't compile when referencing its own properties and members - c#

When upgrading a dotnet Web Site based on asp.net WebForms to a Web App I'm having trouble with Master pages.
My Master pages contain, in their .aspx file markup, references like this:
<div>
Name: <% =GetUserName() %> Account: <% =AccountId %>
</div>
GetUserName() is a protected method defined in my master page's codebehind file, and AccountId is a protected field in the same place.
After doing the Web App upgrade (which involves automatically generating a Site.Master.designer.cs codebehind file to accompany my own Site.Master.cs codebehind file) compilation of the Master page (not the codebehind) starts to fail, rejecting the protected member references in the <% =GetUserName() %> constructs. VS 2015 Intellisense says GetUserName not defined in this context.
But the Go To Definition... item on VS2015's context menu still works for the offending protected member references. Weird.
When I make a new Web App project from scratch, it doesn't have this problem. How can I fix this? Or is there something special about upgraded Web Site projects that prevents these protected member references?

I figured this out.
The order of items matters in the project.csproj, and the conversion from Web Site to Web App doesn't always get it right.
To fix, close up VS2015, then use a text editor to open your project.csproj file. (It will be named for your project.) Look for the <Compile...> tags for your Master page's codebehind files.
Then make sure the tag to compile your own codebehind file comes before the one for the .designer.cs code, like this:
<Compile Include="Site.Master.cs">
<DependentUpon>Site.Master</DependentUpon>
<SubType>ASPXCodeBehind</SubType>
</Compile>
<Compile Include="Site.Master.designer.cs">
<DependentUpon>Site.Master</DependentUpon>
</Compile>
In my experience, these tags were widely separated in the automatically generated .csproj file.
At any rate, moving one of them so they were in order resolved my problem.
Don't forget to Clean and Rebuild your project in VS2015 if you change your .csproj files outside the IDE.

Related

The name does not exist in the current context

I have the following HTML in an .aspx:
<div ID="divText" runat="server" style="position:absolute;top:60px; left:800px; width:600px; height:100px; z-index:2;font-size:200%">
</div>
Then in the code behind, IntelliSense finds the 'divText' but I get the compile error listed in the title
string productEdition = ConfigurationManager.AppSettings["Club"];
divText.InnerHtml = productEdition;
The compile error:
Error 3 The name 'divText' does not exist in the current context
The thing is the same EXACT html and code work in another file. We have tried everything. Any ideas?
In case other suggestions don't work, delete designer file, right click the markup file and click "convert to web application".
This error usually occours when you have copy pasted an aspx file. Please verify that your aspx is pointing to the correct cs file. Also check the names of the two files.
Other option is that there is something wrong with your designer file. If you want to Visual studio to regenerate your designer.cs file, you can go into design mode, make a small change and save the file.
Let me know if none of this works (90% of the time this issue is caused by this).
I had a similar issue when getting old VS2005 aspx/aspx.cs files into a new project in VS2013. I resolved this by creating a new web application, creating a new webform for each aspx page and copying the code for .aspx and .cs. Once copied I have to change the .cs files first line to use CodeBehind instead of CodeFile .
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="_Login" %>
TO
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="_Login" %>
I hope this helps someone else.
Most likely the control variable did not get added to the aspx.designer.cs file. You can re-add the control and Visual studio might add it for you or you can add it manually to the designer.
protected global::System.Web.UI.HtmlControls.HtmlGenericControl divText;
Right-click on the ASPX (or ascx) file, and select Convert to web application (or something like that). That will force a refresh on the designer file.
and you can right-click on project in solution explorer and select to web application. in this case all of your project files will be change.
if you can't see convert to web application after right click, you can click on project menu and you see convert to web application in bottom
I think the problem mix of 3 issue in the middle:
check if namespace in the code MyFile.aspx.cs file is the same of project.
in the MyFile.design.aspx.cs the namespace should match with previous file
if the design file of your aspx page in the partial class have declared the control that give that error.
In my case the issue appeared after copy/added the existing aspx file.
After checked if all namespace are correct the issue was solved.
Looks like your problem may be the uppercase ID which is not setting the id of your element. Also not sure how the id becomes an object given the code you posted.

Finding what method is called

I have an ASPX page with no code behind (that is, no .aspx.cs with the same name). In the code is this line:
<div style="overflow: hidden; text-align: center; z-index: 105;">
<%= MainNavBarHTML %><%= SubNavBarHTML %>
</div>
I've searched the rest of the program for MainNavBarHTML and SubNavBarHTMl, but can't find any reference to them. How do I find what fills those elements?
You might want to check out the CodeFile vs CodeBehind question.
If your ASPX markup has the CodeFile directive, it will look for the associated .cs file:
<%# Page
Language="C#"
CodeFile="CustomerDetail.aspx.cs"
Inherits="SomePage" %>
If, instead, it has the CodeBehind directive listed it will look in the Bin folder for an assembly that has the class defined:
<%# Page
Language="C#"
CodeBehind="CustomerDetail.aspx.cs"
Inherits="SomePage" %>
The naming of these two directives is beyond unfortunate. If the application is using CodeBehind (which it sounds like it is) you may not have access to the source and will be unable to view the definition for those properties, short of using a .NET reflection tool against the compiled assemblies.
In Visual Studio, Put your cursor on the text and press F12 to jump to definition or right-click on the text and choose "Go to definition".
Adding this as an answer now (rather than in the comments).
Your project will have the code somewhere, though it may be part of another library (DLL).
If you right-click on the property (in this case either MainNavBarHTML or SubNavBarHTML, and from the context-menu select "Go to definition" VS will show you either the code (if it's in a *.cs page) or load the object browser and navigate to that property, allowing you to see exactly where the property is located.
Depending on your VS settings F12 may do nothing - does nothing on mine for example. Right-clicking and choosing "Go to definition" is the most stable way to navigate - in my opinion.
Boiler, the code is still there, you just have to select the file and hit F7 and you will see the code...

Migrating from ASP.NET 4 website project to web application project in Visual Studio 2010 Causing all controls to throw error?

I recently updated my VS2010 website project from .NET 3.5 to 4.0. Everything was working fine in the website project. Today I decided to migrate the website to a web application project as I have learned this is the best way to work in .NET. I split out all my class files into a separate class library and copied all my other content into my new project. Then I updated all the references and web.config.
When I build the class library, everything works great.
The problem is happening when I try to build/debug the web application project. It is acting like all the controls are missing and it is also throwing a bunch of compile errors about the public properties I have in my master pages.
Control errors:
"The name 'INSERT CONTROL NAME HERE' does not exist in the current context"
Master page errors:
'System.Web.UI.MasterPage' does not contain a definition....
It is giving these errors for every single control and master page property in my entire solution.
I notice when I add a new web.form to this project, it also adds a filename.aspx.designer.cs file in addition to the .aspx and .aspx.cs file. My existing files do not have these extra files since they were created in a different .NET version.
Anyone have an idea on how to overcome these issues?
UPDATE: It seems I was missing the step where I need to right click on the new application folder and select "Convert to web application". I just did that and it seems to be a little bit better...
Now it is choking on Literals that are inside single quotes:
<div class='<asp:Literal ID="CssClassLiteral" runat="server"></asp:Literal>'>
It doesn't see this literal when it does the conversion... Is the above valid code or should I implement that functionality another way?
Yes - one of the main differences between the website project and a web application project in Visual Studio is that the web application project defines a designer.cs file for every page/user control.
So, let's say you have a page in a website with a codebehind:
Default.aspx
Default.aspx.cs
In a web application, the designer is now required:
Default.aspx
Default.aspx.designer.cs
Default.aspx.cs
The designer file is auto-generated, but you may need to "touch" each page to generate.

Code behind file not recognizing controls in *.ascx

I have a QuestionControl.ascx and a QuestionControl.ascx.cs code behind file I copied to a new project. When I build the project any references in the code behind file to controls declared in the ascx gives me this error:
'QuestionControl' does not contain a
definition for 'rdbtnlstQuestion1' and
no extension method
'rdbtnlstQuestion1' accepting a first
argument of type 'QuestionControl'
could be found (are you missing a
using directive or an assembly
reference?)
This is at the top of my *.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="QuestionControl.ascx.cs" Inherits="QuestionControl" %>
I've also tried CodeBehind:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="QuestionControl.ascx.cs" Inherits="QuestionControl" %>
This is the top of my class in the codebehind file, it is not contained in a namespace:
public partial class QuestionControl : System.Web.UI.UserControl
{
Try deleting your designer file. VS.NET should recreate it for you when you open the ascx file. I've had problems like this in the past where the designer gets out-of-sync for some reason, and deleting it usually fixes the problem.
Another solution is to:
open your .ascx page in design view
right click anywhere on the page and select Refresh
(.ascx.designer.cs file may need to to be closed while doing refresh for this to work)
In VS2017 there is no option 'Convert to Web Application' in the context menu of the .ascx file. Instead you need to select to .ascx file then click on 'Project' in the upper menu and select 'Convert to Web Application' (which is all the way down in the Project menu.
What worked form me was listed on another SO answer and I can't find it so I'm repeating it here.
Try deleting your "ReflectedSchemas" folder in
C:\Users\YOURUSENAME\AppData\Roaming\Microsoft\VisualStudio\15.0_079f391b\ReflectedSchemas
This worked for me. Sometimes I get the "unrecognized" errors, and I delete this folder again. Many thanks to the OP. Been driving me crazy for years. Now I have no "squiggles" in the HTML and no "red bars" in the code behind (aspx.cs)
I had problems for example creating a dropdownlist inside a gridview. What I did is creating the ddl outside of the gv until the desinger.cs recognized it and afterwards moved the control inside the gv... hope this helps
I was having the same issue, the code-behind didn't recognize the controls on the .aspx page. I'm using VS 2012. I right-clicked the project; clicked on Convert to Web Application; and it added all the designer files that weren't there at all before. I rebuilt everything and it's good now.

Asp + code behind: reference missing?

I'm currently working on a project I've just received that is asp.net + vb.
I have to add a gridview in one part of the page, but it simply won't let me set the datasource
<%# Page Language="VB" MasterPageFile="~/Common/Common.master" title=whatever" %>
<%# Register TagPrefix="uct" TagName="SubmenuControl" Src="whatever.ascx" %>
this loads the masterpage and a simple menu.
I had to create a page, so I've based myself on the existing ones:
somepage.asp
Based on othes pages, I've copied the code behind insertion method:
<%# Import Namespace="somelibrary" %>
<%# Import Namespace="otherlibrary" %>
<script runat="server">
'some vb code
</script>
But when I compile, I get the message:
alt text http://dl.dropbox.com/u/3045472/ex.png
Did I forget something? I use simple system references (IO and DATA) it should work without any adition, I've added anyway the .data reference, but it doesn't work, so, what should I do ?
Since there's no vb coding in this question, you could answer it in C# or VB if any addition is needed in the code behind.
info:
0-Visual Studio 2008
1-Works without this page
2-VB.NET but you can use C#
3-I'm new to asp, don't freak out
4-Without the references, the objects that use those references aren't recognized (underlined as reference missing) so the references load OK in theory.
5-If instead of adding the reference in the beginning I give the complete path to the object (ex.: system.io.fileinfo) I get the exact same error.
6-I'm watching this question, anything else you need to know, comment.
You aren't compiling, you're running in debug mode. The startup project you have set is not the ASP.NET project. In Solution Explorer, right click on the web project itself, and select "Set as startup project". Or to simply compile, try Ctrl-Shift-B.
Visual Studio often "runs" whichever project you have open in the text editor (depending on how you invoke the run/build command).
Try opening the default.aspx page in the text editor before you run/debug the project.

Categories