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

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.

Related

How do I stop the VB compiler from running in C# ASP.NET 4.5 Web Forms site?

I am working on an ASP.NET Web Forms site that is written completely in C#. All initial page loads in one subdirectory cause the VB compiler to run. Oddly, page loads in that directory's subdirectories do not cause the VB compiler to run.
There are no elements in the Web.config.
This happens in every solution that includes the project.
None of the Microsoft.VisualBasic assemblies are referenced in the project
Is there a setting somewhere that I can change that will disable VB compiler?
Could this be due to a referenced assembly used in this folder but nowhere else?
Since there seems to be some concern with the validity of this question, I and another senior developer have spent about 10 hours trying to run down this problem. We have searched the code base, visual studio, and online for an answer but it appears to be unique enough that no one has brought it up.
Additionally, when the same code is put in a different directory, the VB compiler does not run on initial page load. This is what leads me to believe there is a setting somewhere that I am missing.
Every time I've seen this, it's because of a "badly"-compiled file that exists in the folder structure (not even necessarily the csproj).
Check the folder structure of the pages in question, see if there's a bad file located somewhere in there. You may have to Show All Files in Visual Studio.
Delete it with extreme prejudice.

Getting C# to recognize a dll outside of Visual Studio

Several years before I started working at this job another developer who is no longer here wrote an application in classic ASP using HTML, vbscript and javascript. This is fine but the problem is that 2 pages were written in C# with an HTML file and a code behind file. There was no solution files for these two pages. They may have been originally created in Visual Studio but they don't exist in it now.
That is important because there is a lot of things that Visual Studio just does for you without even thinking.
My problem is that in these two C# pages I need to get them to reference a DLL. This is a simple task when using Visual Studio. You just add a reference to the project and life is good. But outside of VS nothing seems to work.
I tried putting the dll in the same folder as the pages and then I tried the following:
Using myDLL;
myDLL dll = new myDLL();
myDLL dll = myDLL();
I found some code online that said to create an internal static class and use [DLLImport()] but that didn't work either. It couldn't find the dll or the Entry Point for the dll. I am currently researching how to create an entry point, just in case this is the method to make everything work.
Outside of having to rewrite these pages in vbscript (which I don't have the time to do) I am at a loss.
Has anyone ran into this problem before? Is there something that I can put in the web.Config? Or is this just impossible and I am hosed.
BTW this is all running under the 2.0 .net framework.
If you drop the DLL you want the code to reference into the bin folder of the website, then open the web.config and locate the following section configuration -> system.web -> compilation -> assemblies.
You need to add the display name of the assembly to that list - so that the compiler will reference that assembly during it's late-bound build process.
Now you should be able to use the stuff that's in it on those pages.
If you don't the know the display name of the assembly (typically yourassembly, version=*.*.*.*, Culture=neutral, PublicKeyToken=null for culture-invariant, non-strong-named assemblies) you can open it in a tool like ILSpy (there are others, it's just become my favourite) and it tells you when you select it in it's UI:
sorry for the poor highlighting - jerky hand following far too much coffee
If all the code in that assembly is in a single namespace, also, you can also add a default using to all the .cs or .aspx code in the project by adding that namespace to configuration -> system.web -> pages -> namespaces - making it simpler to use that code in the pages.
I created a VS Solution/Project for my app. I compiled and published it to the web server. When I published it I had it copy all project files.
I ran it and it crashed because it could not find my dll.
I tried adding the lines that Andras mentioned above and it seemed like it was getting me closer but it only changed the errors I was getting.
Then I went into IIS on the web server. I expanded the folder listing under Web Site. I right clicked on the folder that contained my app and made that folder into an application folder.
After I did that everything just worked. So then I thought I would see what happened if I backed out all of the additional code I added to my C# app and the Web.Config file. It still worked. All I needed to do was to make the folder an application folder in IIS and put a Using statement in my C# app and life is wonderful again.
Thanks for all the comments and suggestion. Andras thanks for the link to ILSpy. That is a cool little tool.
Take care,
Robert
I agree with Jon, it sounds like you should try creating a new project for these files. It's always better to leave code better off than you found it. If a new project is not an option for some reason, you should indicate this in your question.

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.

Visual Studio 2010 Disable Designer Code Generation for particular ascx controls

I am currently developing a website for a Berkeley Club in Visual Studio 2010 Prof. Ed. using Asp.net and Dotnetnuke v6. This will be my second time encountering this specific problem/hassle. I created a new control (ascx file) and have been working on it some. I wanted to add the Dotnetnuke htmleditor since its sick so added the required code. Namely...
<%# Register TagPrefix="dnn" TagName="TextEditor" Src="~/controls/TextEditor.ascx"%>
<dnn:TextEditor ID="EmailContent" runat="server" Height="400px" Width="100%" />
I also had to change the ascs.designer.cs file so that instead of reading
protected global::System.Web.UI.UserControl EmailContent;
it read like
protected global::DotNetNuke.UI.UserControls.TextEditor EmailContent;
This makes it use the DNN TextEditor which is what I want. The problem is that whenever VS uses the designer to autogenerate code it overwrites the bottom line with the top line. This means every time I change the ascx file I have to change the designer again. This has gotten annoying, but I have not been able to find a way to fix it. Any ideas on how to disable the VS designer for specific controls or for segments of code in a autogenerated designer file?
Put the EmailContent declaration in the ascx.cs file, and remove it from the ascx.designer.cs file. This will prevent the designer from messing up the type.
This goes to ascx.cs:
protected global::DotNetNuke.UI.UserControls.TextEditor EmailContent;
The problem is that Visual Studio is interpreting the path ~/ to mean the root of your project, instead of the root of the DotNetNuke website (how it will resolve at runtime).
If your module project that you're working on is located in a DotNetNuke site which is hosted in your local IIS, you can setup the project to know where it's actual root is. In the project's properties, go to the Web tab and make sure that it's setup for IIS. The project URL should be the URL to your module (e.g. http://mysite.dnndev.me/DesktopModules/MyModule) and then check the "Override application root URL" checkbox and enter the website's root there (e.g. http://mysite.dnndev.me). This should allow Visual Studio to realize where ~/controls/TextEditor.ascx points, and find the right type.
That said, this works for us about 90% of the time, but we've had some projects where this doesn't work for whatever reason, and then we resort to #mika's answer of moving the declaration from the designer file to the codebehind file.

The name 'controlname' does not exist in the current context

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.

Categories

Resources