The name 'controlname' does not exist in the current context - c#

I have a web application that I'm working on (ASP.NET 2.0 with C#, using Visual Studio 2005). Everything was working fine, and all of a sudden I get the error:
Error 1 The name 'Label1' does not exist in the current context
and 43 others of the sort for each time that I used a control in my code behind page.
This is only happening for one page. And it's as if the code behind page isn't recognizing the controls. Another interesting thing is that the IntelliSense isn't picking up any of the controls either..
I have tried to clean the solution file, delete the obj file, exclude the files from the project then re-add them, close Visual Studio and restart it, and even restart my computer, but none of these have worked.

I know this is an old question, but I had a similar problem and wanted to post my solution in case it could benefit someone else. I encountered the problem while learning to use:
ASP.NET 3.5
C#
VS2008
I was trying to create an AJAX-enabled page (look into a tutorial about using the ScriptManager object if you aren't familiar with this). I tried to access the HTML elements in the page via the C# code, and I was getting an error stating the the identifier for the HTML ID value "does not exist in the current context."
To solve it, I had to do the following:
1. Run at server
To access the HTML element as a variable in the C# code, the following value must be placed in the HTML element tag in the aspx file:
runat="server"
Some objects in the Toolbox in the Visual Studio IDE do not automatically include this value when added to the page.
2. Regenerate the auto-generated C# file:
In the Solution Explorer, under the aspx file there should be two files: *.aspx.cs and *.aspx.designer.cs. The designer file is auto-generated.
Delete the existing *.aspx.designer.cs file. Make sure you only delete the designer file. Do not delete the other one, because it contains your C# code for the page.
Right-click on the parent aspx file or Project menu. In the pop-up menu, select Convert to Web Application.
Now the element should be accessible in the C# code file.

Check your code behind file name and Inherits property on the #Page directive, make sure they both match.

exclude any other pages that reference the same code-behind file, for example an older page that you copied and pasted.

I had the same problem. It turns out that I had both "MyPage.aspx" and "Copy of MyPage.aspx" in my project.

Also, make sure you have no files that accidentally try to inherit or define the same (partial) class as other files. Note that these files can seem unrelated to the files where the error actually appeared!

I ran into this same error, except it was a WPF error. I was rearranging projects and had a control defined in like this:
<local:CustomControl Name="Custom" />
In my code behind I tried using Custom.Blah, but I got the error:
The name 'Custom' does not exist in the current context
What did the trick for me was changing my control in Xaml to this:
<local:CustomControl x:Name="Custom" />
Hope this helps someone out there!

I get the same error after i made changes with my data context. But i encounter something i am unfamiliar with. I get used to publish my files manually. Normally when i do that there is no App_Code folder appears in publishing folder. Bu i started to use VS 12 publishing which directly publishes with your assistance to the web server. And then i get the error about being precompiled application. Then i delete app_code folder it worked. But then it gave me the Data Context error that you are getting. So i just deleted all the files and run the publish again with no file restrictions (every folder & file will be published) then it worked like a charm.

I had the same issue, my problem was not having space between two attributes"
AutoGenerateColumns="False"DataKeyNames="ProductID"
instead of
AutoGenerateColumns="False" DataKeyNames="ProductID"

I fixed this in my project by backing up the current files (so I still had my code), deleting the current aspx (and child pages), making a new one, and copying the contents of the backup files into the new files.

this error often occurs when you miss runat="server" .

I had the same issue since i was tring to re produce the aspx file from a visual studio 2010 project so the controls had clientidmode="Static" property. When this is removed it was resolved.

I had a similar problem when tweaking with a Repeater after converting it from a DataList.
Problem was that I accidentally united 2 attributes when deleting an unneeded one.
<asp:Repeater runat="server" ID="ClientsRP"DataSourceID="ClientsDS">
.
.
.
</asp:Repeater>
And this prevented the generation of the repeater in the design file.

I had the same error message. My code was error-free and working perfectly, then I decided to go back and rename one of my buttons and suddenly it's giving me a compile error accompanied by that blue squiggly underline saying that the control doesn't exist in current context...
Turns out Visual Studio was being dumb, as the problem was related to the backup files I had made of my aspx.cs class. I deleted those and the errors went away.

In my case, when I created the web form, it was named as WebForm1.aspx and respective names (WebForm1). Letter, I renamed that to something else. I renamed manually at almost all the places, but one place in designer file was still showing it as 'WebForm1'.
I changed that too and got rid of this error.

1) Check the CodeFile property in <%#Page CodeFile="filename.aspx.cs" %> in "filename.aspx" page , your Code behind file name and this Property name should be same.
2)you may miss runat="server" in code

In my case I had to hunt through the 417 "controlname not found" errors to find an actual error: I had replaced a DLL but not updated the version number in the web.config. Fixed that and built successfully, 3 minutes after that all the other errors had resolved themselves.

Solution option #2 offered above works for windows forms applications and not web aspx application. I got similar error in web application, I resolved this by deleting a file where I had a user control by the same name, this aspx file was actually a backup file and was not referenced anywhere in the process, but still it caused the error because the name of user control registered on the backup file was named exactly same on the aspx file which was referenced in process flow. So I deleted the backup file and built solution, build succeeded.

I ran into this same issue. Apparently, you shouldn't call a class in the DLL the same name as one of the .aspx/.aspx.cs files. I thought they would not be in the same scope, etc. but it messed with Visual Studio's internal workings too much. I'm a bit surprised there isn't something to keep you from doing this if it is going to produce that type of error. Anyway, just delete the .aspx/.aspx.cs files and rebuild your project. Then bring them back in under another name. You can copy/paste your code into another editor if you don't want to retype it all back in.

Related

User temp AppData error while publishing project in VisualStudio 2012 [duplicate]

When running a web application project, at seemingly random times a page may fail with a CS0433 error: type exists in multiple DLL's. The DLL's are all generated DLL's residing in the "Temporary ASP.NET Files" directory.
Add the batch="false" attribute to the "compilation" element of the web.config file.
This problem occurs because of the way in which ASP.NET 2.0 uses the application references and the folder structure of the application to compile the application. If the batch property of the element in the web.config file for the application is set to true, ASP.NET 2.0 compiles each folder in the application into a separate assembly.
http://www.sellsbrothers.com/1995
http://support.microsoft.com/kb/919284
This might happen if you place .cs files in App_Code and changed their build action to compile in a Web Application Project.
Either have the build action for the .cs files in App_Code as Content or change the name of App_Code to something else. I changed the name since intellisense won't fix .cs files marked as content.
More info at http://vishaljoshi.blogspot.se/2009/07/appcode-folder-doesnt-work-with-web.html
One possible reason for this error is that there are 2 aspx pages which are having the same name in their inherits= in the <#page language=......inherits=> line.
Changing the inherits= name solves the error.
Just in case someone else shares my problem, I got this error when trying to publish a Web Site of a newly branched project, build worked perfectly.
Turns out I had forgotten to remove the checkbox for "Allow precompiled site to be updatable" under publish Settings -> Configure precompile.
As another data point, I just had this problem without any evidence of circular references as described in the links in Ben's answer. Building my web site project would fail with a few of these errors, and setting compilation batch="false" fixed it, but I didn't want to go that route as this is a large-ish production website.
This solution was in a subfolder of my D:\svn folder, which I had mapped to S:. When I opened the solution from S:, these errors occurred, but if I went straight to D:\svn and opened the solution, no errors.
I also noticed that, despite having compilation batch="true" in my web.config, when opening the solution from the mapped S: drive all my .ascx files get compiled into their own assemblies. If I open it from the physical location, the .ascx files get compiled into their respective folders' assemblies (which is how batch="true" is supposed to work).
Strange.
This error was due to conflict between class name of web form and wsdl stub(code behind file .cs) having the same class name i.e.
ASPX page: Dashboard
Class: partiacl class Dashboard
AppCode/APIServices.cs: public partial class Dashboard
Error was reproducible only on publishing the website but build and debug did not inform any error.
In my case deleting all output assemblies from bin folders in all projects in the solution solved the issue. Unfortunately I have no explanation for it.
In my case I had renamed a project, so also the dll had been renamed. When I just copied the new dll but didn't think of deleting the old one from the server, I soon had a bunch of pairs of classes with the same names. Deleting the outdated dll's was doing the trick (of cause).
None of these answers worked for me, however I did fix the problem. Since I was using VS's Publish function to deploy the web application, I selected the option to delete all existing files prior to publish in the Publish Web wizard. This forced a clean copy of the application and everything worked fine from there.
This solution might be helpful if your local debugging copy works fine but published system isn't. Also great if you don't want to take the time to track down individual dlls to delete and don't mind the production files being deleted first.
In my case, the problem was solved when I edited a Designer.cs file that still had the duplicated class name. for some reason, when i renamed the class "logout" to "logout2", in the designer file it was not automatically changed, and was still "logout", and this class name already existed in a precompiled dll in my project (that belongs to a third party web app that I work with and develop around of).
Got this problem when put a part of an aspx page into the separate user control. On my machine everything was fine, on the server got an error.
Renamed the problem class and file.
http://support.microsoft.com/kb/919284 Method 2: Reorder the folders in the application is writing about possible circular references
None of these solutions worked for me. Both of my conflicting DLLs were in C:\...\AppData\...\Temporary ASP.NET Files\...
The problem was that I had rolled back my source repo to an earlier version - before we moved a type from one project to another project within the same solution.
I tried deleting the newer DLL - which should not have even been there at all in the older codebase - from the "Temporary ASP.NET Files" location identified by msbuild. msbuild just put it back.
I also tried the web.config setting that some here have used successfully, but that did not work either. Although, as I write this, I realize that there were actually two MVC projects within the same solution and both had errors, so the problem may have been that I did not add the setting to both.
I tried rolling my source repo forward and cleaning and rolling back again and cleaning. Nothing.
I tried deleting everything the "Temporary ASP.NET Files" location. msbuild just put it back again.
Finally, I tried rebuilding in Visual Studio. Although the command line output and the "Errors" output both gave the same msbuild "Temporary ASP.NET Files" error, the Intellisense error - when hovering over the conflicted type - actually complained about DLLs in output directories. Apparently "Clean" and "Rebuild" were not doing their jobs. I manually deleted the DLLs in the output directories identified by Intellisense, and the problem was solved.
tl;dr - Make sure you're covering all of your web.configs with the batch setting, and try to leverage Intellisense for further clues.
My problem was linked to a .dll that was getting generated in my project folder.
If you are referencing another file, instead of doing everything you see above, what fixed my problem instantly was just deleting the .dll that was staying inside my /bin directory for my project.
The problem isn't necessarily a web.config fix - it's a circular reference that needs to get resolved. I realized that I cleared the old .dll in my original project file but not in the project that was referencing it.
I don't recommend making the modification to your web.config file because that's just a band-aid fix - not really addressing the actual problem. Do that if you don't feel like fixing the problem, but if you want to avoid future headaches, just remove the .dll from both places.
I had a partial class with the same name in two different projects.
I solved it by only leaving it in one project.
None of this solutions worked for me. Compiling in "Release" mode worked, but when I switched to "Debug" I got umpteen of this error Messages.
I don't understand why, but a simple restart of Visual Studio was my solution.
Sometimes it may help to remove the solution and create it again.
Since this use to happen when converted from VS2005 to vs2010 some references to framework 4.0 (after upgrading ) remains in the solution, even all projects are defined as 3.5.
Normally rebuilding the solution should clear these problems.
I had the same problem when I was compiling the application on a compiling server.
My controller had a simple static code, so I changed my ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="controllerName.ascx.cs" Inherits="Controls.controllerName" %>
To
<%# Control Language="C#" AutoEventWireup="true" Src="controllerName.ascx.cs" Inherits="Controls.controllerName" %>
Also removed the partial keyword from the codebehind and added a namespace to the codebehind.
This:
using System;
using System.Web.UI;
/// <summary>
/// My controller
/// </summary>
public partial class controllerName: UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
To this:
using System;
using System.Web.UI;
namespace Controles
{
/// <summary>
/// My controller
/// </summary>
public class controllerName : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
And that worked for me.
For me this happened when I had my PrecompiledWeb/Publish location set to the current directory which was where the site's root folder was too.
My Web Site was then seeing the publish folder as part of the project when compiling/building and then finding duplicates in that manner.
i.e. Don't put the published/precompiled version of your site in your site's code folders.
If the DLL's are showing in a temporary folder, you should try cleaning your solution.
Posting my solution:
The issue was related to the "On-Access Scan" of Mcafee Antivirus. Disabling this solved the problem. Somehow, the ASP Temporary folder was not being used properly by ASP when the antivirus was ON.
Hope this helps someone.
App_Code folder is causing the problem , put the class outside the folder (Works fine)
App_Code folder is not designed for Web Application Projects
http://vishaljoshi.blogspot.in/2009/07/appcode-folder-doesnt-work-with-web.html
Go to Add reference and search for both the dll,
Both of the dll would have checked, uncheck one of the dll, as there are references to the same dll with different version ambiguity gets generated.
My solution was to replace CodePage="...." with CodeBehind="..." in the .aspx file. Somehow it was left as CodePage during a migration from previous .NET versions.
This page directive creates another dll file which conflicts with the projects dll file.
I faced with the problem in compile time.
I agree with the batch="true" attributes, error is telling there exist 2 assembly
Solution 1: deleting one of them
Solution2: Configure one of them
Had a similar problem, In my case, I noticed, that cleaning a solution doesn't clear the bin folder in the visual studio. There was old compiled .dll present in the folder that is causing the issue.
Solutions:
Manually delete bin folder and recompile
In case of publish, select delete existing files prior to publish.
This will solve the issue.
You should define an alias for one of your references.
In your project file .csproj add the following item:
<ItemGroup>
<Reference Include="temp1.dll">
<Aliases>MyAssembly</Aliases>
</Reference>
</ItemGroup>
After adding the above ItemGroup, MyAssembly will represent a root namespace that will contain all namespaces in the assembly temp1.dll.
Then you can have access to the type foo, which is located in temp1.dll, as follow:
using MyAssembly.foo;

SharpDevelop - Broken link between .aspx and .aspx.cs files

I have just started using SharpDevelop and I am trying to convert a Visual Studio Express website project into a SharpDevelop one. I managed to create the solution fine and the project within it. This is a C# ASP.NET webforms project by the way. Just simple aspx pages with C# codebehinds and an .asmx webservice in there. Nothing special and in fact quite old fashioned stuff, which worked fine in Visual Studio.
I am liking the SharpDevelop environment so please don't answer with "why not use Visual Studio instead" (I have reasons). But here is my problem:
SharpDevelop is not linking the .aspx files with their .aspx.cs counterparts. So instead of the solution explorer showing MyPage.aspx which you then expand to see MyPage.aspx.cs under it, you see them as two distinct separate files next to each other and unrelated.
And when I compile the solution I get an error because the codebehind can't tie itself to the aspx page and doesn't recognise the control names. (Example below is from my Contact.aspx page but it happens on every page where I have any server-side asp.net control).
The name 'EmailAddress' does not exist in the current context (CS0103) - Contact.aspx.cs:52,100
Now keep in mind this never used to happen in Visual Studio, so something has gone wrong in my SharpDevelop solution. The error occurs anywhere in the codebehind (.aspx.cs file) which refers to:
EmailComments.Text
Things I have already checked:
1) The #Page directive is correct and the Inherits name matches the class name.
2) The .cs and .aspx filenames and class names all match.
3) The .net framework version is 4.5 (does this make a difference?)
4) I have tried explicity declaring the asp.net controls in the codebehind and yes this prevents the error however the link between files remains broken and why should I have to declare the control in the codebehind anyway? Never used to in Visual Studio and it worked fine there.
Wierdly there is one file in my entire solution which does not have this problem (the aspx and aspx.cs files are linked fine) and this is the default.aspx page. There is nothing different about it. The only thing I can see is that it's a lowercase filename whereas the others are mixed case. (Surely that's not relevant?)
I would really appreciate help from anyone familiar with SharpDevelop or if it's a more general .net issue then please tell me. I feel as if there is some basic thing I am forgetting that will make this work.
Thank you very much for any help.
UPDATE: I have managed to link the .aspx and .aspx.cs files by doing the following: Drag and drop the .aspx.cs file on to the .aspx file in the solution explorer. Do this for every pair of files. Then save and close the solution and re-open and they remain linked. BUT, the error remains as per above because the codebehind does not recognise the controls referred to in it.
You may have a website or a "web application" project. There are differences in how code-behind is wired up between the two. Most importantly you may need CodeBehind instead of CodeFile attribute in your page directive. See http://msdn.microsoft.com/en-us/library/vstudio/dd547590(v=vs.100).aspx#summary_of_differences
Hope this helps.

Parser Error Message: Could not load type 'sometype'

I am experiencing an error that I am unable to resolve for some time now. I was wondering if someone can help identify the cause of this error? I am completely new to asp / asax. After some research, I think that the error I am getting is due to the web application trying to use outdated code. I was thinking to rebuild the c# file using Visual Studio and/or the entire project. However, I am completely new to C# and asp, and was wondering can give me some suggestions if this may fix the problem and/or if there is an possible alternate solution.
Error message
Parser Error Message: Could not load type 'Inventory1.Global'.
Source Error: <%# Application Codebehind="Global.asax.cs" Inherits="Inventory1.Global" %>
Entire Global.asax contents:
<%# Application Codebehind="Global.asax.cs" Inherits="Inventory1.Global" %>
Try replacing CodeBehind with CodeFile
Could not load type
means that a type could not be loaded. (In this case, "type" refers to Inventory1.Global). Types are located in compiled DLLs. So, either the DLL isn't available, is out of date, or doesn't contain a public type with the given name.
Some possible causes are:
You have no type declared with the given name. For your example, you should have the following:
namespace Inventory1 {
public class Global {
...
}
}
Note: avoid names like Inventory1. They imply that there is an Inventory2, Inventory3, etc., which is bad practice as they're abmiguous and not very descriptive. Also, Global is pretty vague, and may introduce confusion with the global namespace.
Make sure your cases match (Inventory1, not INVENTORY1.)
You haven't compiled the project. In VS, rebuild the solution.
The assembly that declares the class has a compilation error, so the relevant DLL is either missing or out of date. Make sure you've resolved all errors.
The class is not marked as public.
If I had to guess, I'd put my money on a compilation error. Unlike PHP and other interpreted languages, C# have to be successfully compiled before they can be used.
I had this error , just needed to rebuild the project
I faced this issue and i got the solution from here and i would like to share it.
SOLUTION
Empty the bin folder. Build all the dependent class libraries and refer them in the main project and build the complete solution.
I did this and it worked like a charm for me !!
After scouring around for what could have caused this I found a few things that I needed to do to get my project running...
(Note: You may not need to do all of these - it is a case-by-case thing)
If you did any changes from IIS Express to Local IIS you may need to change the build configuration from bin/debug to bin. (Right click on solution >> Properties >> Build >> Output)
If you have a URL rewrite then you will need to install URL rewrite on your Local IIS.
Navigate to your applicationhosts.config file (usually it's some place like C:\Users\username\Documents\IISExpress\config) and rename the file to applicationhostsOLD.config.
Clean and rebuild your project. You may need to go manually empty out the bin.
Now you should be good to go.
Since it was only happening with IISexpress, changing output from bin\Debug\ to bin\ solved it for me. Changing tag CodeBehind to CodeFile only created even more problems.
This happened with me on my local machine. The issue was incorrect IISExpres config.
If you are getting this issue on your local environment (Visual Studio debug runs), check the IIS Express config file. Make sure your local site/application path is pointing to the correct location.
The configuration file is called applicationhost.config. It's stored here:
My Documents > IIS Express > config . Usually (not always) one of these paths will work:
%userprofile%\documents\iisexpress\config\applicationhost.config
%userprofile%\my documents\iisexpress\config\applicationhost.config
It can't find the necessary file in dll assembly.
Rebuild the project, Rebuild the solution and then try it again.
I added a new build profile and that defaulted to output of
/bin/[new profile name] and when i was running debugger it was trying to look to just /bin
It's likely that you renamed something. Check the Global.asax.cs file for the class declaration and make sure that the namespace and class name match exactly what's in the asax file. This includes case! Can you copy/paste the namespace and class declaration of the .cs file into a post here so that we can compare?
Parser Error Message: Could not load type __
After doing everything suggested in the comments above, with no luck, refreshing (uploading) the contents of /bin to the server worked. The files uploaded to bin are the: dll, pdb and xml. Don't know which one did it.
The problem I had here was induced by renaming a file (_.aspx) in Solution Explorer.
Rebuilding/re-publishing my project/solution to the server did nothing to help me, and I doubt that will help that many out of this predicament. For me, I did a few things to troubleshoot this that eventually got me out of this "hole".
I had been trying to use a binding on the web site, but this wasn't working. I tried calling the site with http://localhost/Report.aspx (this was my homepage, which I opted to not call Default.aspx - I was going to update the "Default Documents" section with the name later) when I got the Parser Error the OP saw. So I tried some things:
I stopped the old project's website and built another, simple web project, that had "hello" and a label on the page and nothing else. I had a line in the Page_Load to populate the label's Text property with "world!", just to make sure that part was working. I created a new website on port 80 and transferred the published contents of my site to the server. So even though I had .NET 4.5 installed on the server (and had ran the aspnet_regiis -i command from the 4.0 directory) and the App Pool in IIS that I was using for this new project was set to 4.0, the browser complained about the web.config having a targetFramework=4.5.2 in it, which is Visual Studio 2015's default framework. So I installed .NET 4.6 (NDP46-KB3045557-x86-x64-AllOS-ENU.exe), restarted the server, and then my simple site worked. So then I deleted this site - all I wanted to do was prove my installation steps were accurate and the server could run a site.
So then I went back to my original project/site - I deleted and re-created the web site. I put the Application Pool to the one I had originally created for this, which I ensured was running .NET 4.0. Once I did this, I navigated to my site and everything worked when using http://localhost/Report.aspx. So it seems to me what causes this is what version of the .NET Framework you are using.
I tried all the solutions listed above and none of them worked. I finally created a new web page (webform) and copy blocked all the code (cs and aspx files) into it from the old one, deleted the old cs and aspx file, recompiled, and now I'm back in business. I know it makes no sense. It should not have mattered, but it worked.
Please try to open your project as Project/Solution, most probably it will resolve the error. This type of error Could not load type.... occurs when we try to open project as website.
I have tried to open my project as solution and it resolved my problem.
Please check namespace and class name at all places, In one case, One team member changed namespace and I was using old namespace in .aspx file. It was causing issue. I updated namespace and it got working.
I was fixing my namespaces in our Base Project, and I started seeing this error on another project that references it after that. I had to remove the reference to the Base Project and re-add it and then it started working again.
I just got this error today. It turns out that it was because I reverted by mistake the project file to an older version that didn't include the page anymore.
I had the same issue after renaming an aspx page Visual studio renamed it but dropped the namespace. Make sure the Inherits property contains the fully Qualified name including the namespace.
If you just added the new aspx File, rebuild the project it is located in. The problem comes from your Code Behind file that isn't compiled at the moment, therefore you want to access a newer page that doesn't exist in your current compiled project dll
I had this problem on the remote server, but not on my local server. After trying everything and nothing working, I finally resolved it. My domain name was pointing to a directory under another domain. I had originally built the website independently in Visual Studio as its own project. No matter what I did, it wasn't working anymore. So I moved it to a folder inside of the project for the main domain name and uploaded it as part of the main project.
For example, I have say domain name AAA.com with a website of its own. And then I also have BBB.com that points to a directory under AAA.com's main directory. Originally I had separate VS projects for AAA.com and BBB.com, but that wasn't working anymore. So I had to move all of BBB.com's files to the AAA.com project and set it up exactly like it appears on the remote server. For some reason, that worked.
Try This It will Definitely work :-
Parse Error:
May be you Class name is not matched with the webform name

Designer.cs gets corrupt after making changes to the webpage in Visual Studio 2010

Visual Studio isn't behaving normally when I make a change in an aspx file. It makes changes to the designer.cs file. These changes mean that I cannot access any of my controls by their ID in my code behind file.
When I revert the changes (using SVN), that have been made behind my back, in the designer.cs file, my build succeeds again and everything works fine.
I see that Visual Studio deletes a lot of lines in the designer file.
I've read some similar issues on the web but I can't find a good solution for this.
Has anyone of you experienced the same problems with Visual Studio and can help me solving this?
Update: I found that Visual Studio adds this line to the designer.cs:
protected global::System.Web.UI.WebControls.UpdatePanel UpdatePanel1;
But this should be:
protected global::System.Web.UI.UpdatePanel UpdatePanel1;
When I make this change manually in the designer.cs file, it works. But every time I make a change to the aspx file, Visual Studio creates again the wrong reference.
im having the same issue. my workaround is moving the definition from the designer file to the code behind file and defining it properly as:
Protected WithEvents UpdatePanel1 As Global.System.Web.UI.UpdatePanel
instead of letting designer keep overwriting it improperly as
Protected WithEvents UpdatePanel1 As Global.System.Web.UI.WebControls.UpdatePanel
im using vs2013 vb.net asp web forms application.
i've also seen suggestions to change the target framework in the web.config file to 4.0 (did not work for me, im targeting 4.5, was getting the problem with both targets)
and i also saw a suggestion to use the Register tag like:
<%# Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web"%>
at the top of the page. this did resolve the 'error' that was being thrown when i tested it, however it caused numerous 'warnings' to appear for all of my asp tags.
My office is plagued by this issue in Visual Studio 2013. We were using the following workaround:
Right-click on the project and select Properties.
On the Application tab, change the Target framework version and save the project.
Repeat steps 1 and 2 to set the target framework back to the one you want.
Save the .aspx file again.
This would work 100% of the time, but we discovered that it messed up some of our web.config settings.
We are now using the following workaround, which also seems to work and does not make any breaking changes to the project config files:
Right-click on the project and select Unload Project.
Right-click on the project and select Reload Project.
Save the .aspx file again.
Make sure that the class name of the cs file is the same as the designer.cs and check the Inherits attribute of the page in the aspx file is the same. Also check if there is any class in your project with the same name as your page class name, also i would like to know the error that appear when you make any updates in the aspx page.

Designer.cs not updating when new controls added to .aspx

I've added a new control to my aspx files and noticed that not only was the new control not added to the designer file but that it was also missing quite a few other controls that were added by other members of the team. I've tried deleting the designer.cs file and using "Convert to Web Application" with no success. Some other things i've tried have been excluding the aspx from the project, building, and then re-including with no success. I've also manually entered in a control that was missing in the designer into the designer. When I run after do so an error appears saying the control isn't defined, even though it actually is, and that I should check if I'm missing a directive.
The first control I added was copy and paste from a similar control and made necessary changes. But, i've also tried manually creating the control with the same results.
Any ideas?
Try this
1.- Change CodeBehind="Name.aspx.cs" to CodeFile ="Name.aspx.cs"
2.- Build
3.- Change CodeFile ="Name.aspx.cs" to CodeBehind ="Name.aspx.cs"
I've been suffered this problem in my work.
The surest thing is you regenerate your .designer.cs file, and here is the solution:
Locate the corrupted aspx.designer.cs file through the Solution Explorer
Delete only the designer.cs file from your project
Rightclick your main aspx file and select “Convert to Web Application“.
For me the solution was to:
Put your page in design view and right click / refresh. It will sync
the controls with the designer.cs. Make sure designer.cs is close
before doing this.
Source
Close Visual Studio, then delete Temporary ASP.NET Files from C:\Windows\Microsoft.Net\Framework\Your_.Net_Version\ProjectName Delete the folder ProjectName and re-start Visual Studio. I had a similar issue some time ago and it was solved by these actions.
If you use IIS, you may need to stop the server/site to be able to delete the temp files.
To replace the designer file in VS 2013:
Delete the old *.designer.cs file
select the *.aspx file
Select “Convert to Web Application“ from the Project top drop-down bar
I did this in VS2012:
Open the page.aspx.designer.cs Find a control declaration of the
same type of control (ie LinkButton)
Copy it to the end of the file (before the end of class curly bracket)
Rename the control variable to match the name of your control.
That's it.
In my case, I had to add the runat="server" attribute then to rebuild the solution. The missing elements were automatically added to the generated aspx.designer.cs and become available for use in code
<button runat="server" id="Button01" type="button" class="Button" onclick="location.href='http://http://www.example.com/';">
<asp:Label runat="server" Text='<%# GetText("Button01")%>'></asp:Label>
</button>
One simple/stupid mistake that also results in similar symptoms is if you copy aspx and aspx.cs files and rename them, but neglect to update the references inside the file.
I came across this question trying to find the temp asp.net directory, but clearing that didn't help. I then realized that I did not update CodeFile="Page.ascx.cs" after I had copied the files to create a new modified version of the page. Then i was trying to add a control and reference it in PageModified.ascx.cs but kept saying it didn't exist.
This also seems to happen when you have a usercontrol that references another usercontrol in the same namespace.
if you move the referenced user control to a different namespace the problem goes away
Sometimes there might be errors in html like two controls having the same Id. Also be careful with div and span tags. These stops the designer from getting updated. For all of these, check the warnings in Error List and fix them. You are ready to go. This solved my issue.
I was able to solve the problem only manually adding the control declaration inside the code behind file. Every time I tried to regenerate the design.cs file, VS 2013 would write the same lines without the missing control.
The declaration needs to be added in the code behind, NOT in the design.cs, because it would be deleted each time the .aspx is changed.
Just add the declaration like you would do for any other variable, something like this:
protected MyNameSpace.MyControl ControlName;
Just to add to the list of possible solutions: These lines in my markup file, although seemingly valid, were apparently causing a disconnect with the designer file:
<%: Styles.Render("/my-file.css") %>
<%: Scripts.Render("/my-file.js") %>
I simply commented them out, saved, and the designed file began working correctly. I then uncommented both lines, saved, and was able to add new controls to the page without issue. I found this strange, and it's still unclear to me what the initial cause of the problem was. I can only speculate that it's a Visual Studio bug.
Point being, there may be perfectly valid code that's causing an issue, in which case, a possible solution would be to go through commenting HTML sections out in your markup file until the designer file resolves itself, and then you can undo all your commenting and resume working as normal.
I tried all of the above in VS2019.
I deleted the designer file, clicked on the Project Menu / Convert to Web Application which rebuilt the designer file, however, it still wouldn't auto regenerate.
What worked - excluded the file from the project, then reincluded it.
The file now keeps auto regenerating.
For creating a code-behind C# file with Visual Studio 2019 from inline ASPX code, I had success with the following procedure:
Create CS file with the same base name and '.cs' extension, using Project/AddClass from the main menu.
Move all C# code from the ASPX file into this new file. Add the following code to this new class:
namespace <<your_namespace>> {
public partial class <<your_class_name>> : System.Web.UI.Page {
Add / modify the 1st line of the ASPX file:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="<<your_class_name>>.aspx.cs" Inherits="EVV.pages_sqlserver.<<your_class_name>>" %>
Create CS file with the same base name and '.designer.cs' extension, using Project/AddClass from the main menu.
Copy the contents of any other .designer file. Remove the complete contents of the class, only curly brackets remain.
Open the form designer for the basic ASPX file. Right-keep any empty area and select Refresh. Sometimes, this is sufficient. If not:
In Solution Explorer right-click the same ASPX file and select Publish.
Sometimes it only worked after closing Visual Studio, deleting all temporary files and reopening the solution.
** Update: in Visual Studio: 16.6.3 this seems to be broken: it sometimes works, sometimes not. Even for completely new WebForms, there is no update on the Designer file. **
** Update 2: This does not work reliably. I switched to Redesigner (https://github.com/seanofw/Redesigner/tree/branches/net40) which is working like a charm.
My designer.vb code file was not updating because of an error in Telerik web controls - I had an incorrect value in the DataKeyNames attribute of a MasterTableView. The aspx editor did not pick this up at design time as it's simply a string value, but it was causing the autogeneration of the designer.vb file to fail.
In my case it helps just to change "ID" attribute of control in ascx file to some other value first, and then back to desired. It leads to refreshing of ascx.designer.cs file. Simple trick in case C# code is not generated for your control yet for some reason...
For me the solution was:
I deleted the designer file.
Recycle bin and restore the same file.
In visual studio solution explorer, click on show hidden items from top and include the same file to the ascx file.
Right click on ascx page and click on view designer. (optional)
On the designer, right click and refresh and save. (optional)
As a solution right clicked on the aspx.cs page and selected move... into and choose the
Make sure your control is in the page's scope. In my case, it was in a Repeater template.

Categories

Resources