Codebehind file doesn't recognize aspx-controls - c#

I was given a webproject written with aspx/c#. When I loaded it into Visual Studio 2010, I got many error telling me that some controls in the code-behind files do not exist in the current context.
I checked for the common pitfalls, like wrong code-behind file name, missing runat-attribute, restart VS, reload project, yet nothing resolves the error.
What else can I do to check where the problem is?

Try to change the code-behinde to CodeFile="where code locate", then it probably will work.

inherit class for the markup file and code behind file should match ,and also make sure if these controls are not third party and may need referencing their dlls or something .If these controls are user controls(ascx) controls , make sure they are using the correct tagname in the register line at the top .

Cudos to #03Usr the following link brought the answer:
ASP.NET controls cannot be referenced in code-behind in Visual Studio 2008
Deleting all designer files and converting the project als web application brought the designer files back, effectively making the controls referenceable again.

This is a really annoying hack but if you delete the control from the aspx page and then save the page, then paste the control back onto the page and save again the code behind then seems to recognize the control. I keep running into this problem whenever I change an attribute like the ID.

In my case, the aspx file was inside a folder named XYZ, and the class name was ABC. So ASP named the C# file's class name as XYZ_ABC
After renaming the file to ABC, the error was fixed
Hope it helps!

Related

In VS 2019 - How to regenerate code-behind and designer files from an existing aspx page

I imported in my project Web c# an aspx page, for example, mypage.aspx, with HTML code. This page is very simple.
All I want is to generate the 2 files (in IDE) aspx.cs and designer.cs (mypage.aspx.cs and mypage.aspx.designer.cs).
I tried "Convert to web project" functionality but it does not work.
Any idea, please?
Of course, I read several solutions on this site but none worked (with vs 2019).
If there is no actual code in the original code-behind file then it's pretty easy to do. Create a new page of the same name. Then just copy and paste the markup from the original, taking care to leave the newly-created page directive as is.
If there was logic in your original code-behind then you're out of luck. You might try decompiling the original DLL and try to re-create the original code that way.

Loading html page from a template on the computer

I am working with a C# on visual studios.
What I would like to know is if it is possible to have a html template loaded from a directory on my computer. Then every time I load up my code it will load up the .html template automatically.
Yes there is a way, not suggested but exist. You can use this code to include the file, inside your asp.net page.
<!--#include file="template.htm"-->
What is dose is that is load this htm file and show it on the place that you have place this declaration. Also you must know that this file can not contain anything dynamically. This is a command code that comes from the old asp script code, and still work on asp.net
So I can not call it as template. If you wish to make something like a real template you must use a master page. Using a master page is far better, and you just copy paste your htm code inside the master page and there you have it.
relative: http://triaslama.wordpress.com/2008/11/19/how-to-include-file-in-aspnet-pages/

How to deploy a single user control which has changes to the cs file (ascx.cs)

I have a user control (UserControl1.ascx) and I made changes to Its cs file.
That UserControl1.ascx is being used by two or more aspx files using LoadControl.
I don't want to deploy the whole web project dll as this is a fix for a critical issue in a part of the web app and doesn't affect the whole web app.
Updating changes to the single web form code behind is easily done. However not sure about the single user control deployment. Any help is appreciated.
it's a bit of a hack but you could move the code from the .ascx.cs file to the .ascx file and remove the inherits attribute. Asp.net first checks the referenced class in the inherit attribute. So I think this should allow you to update the web application without updating the .dll files. I didn't test this however, but I think it could be a viable option.
If you just deploy the updated user control (ascx) form and the relevant DLL, the only discernible delay will be the load time of the first hit after deployment, which you can do yourself with a browser refresh.

How do I fix these C# errors?

I found some code to help me in a project and when I first ran the code I received an error message indicating: "Visual Studio cannot start debugging because the debug target c:\path\'dirInfo.exe' is missing. Please build the project and retry, or set the OutPath and AssemblyName properties appropriately to point at the correct location for the target assembly."
Then I select OK and receive an error message indicating that partial is missing. I add partial to the code and receive 3 more error messages.
The type 'RecursiveSearchCS.Form1' already contains a definition for 'components'
does this mean I should delete this from the Form1.cs file?
Type 'RecursiveSearchCS.Form1' already defines a member called 'Dispose' with the same parameter types.
Type 'RecursiveSearchCS.Form1' already defines a member called 'InitializeComponent' with the same parameter types.
(I notice, when I comment out the InitializeComponent line and/or Dispose line, many more error messages populate in ERRORS)
By they way you can find the original code # MicrosoftSite.
Any help would be greatly appreciated.
Thank You
Just gut instinct, if you were following along and copy pasting remember one key thing:
The designer creates two files when you create a form: A "code" file, and a "designer" file. However, when microsoft (and others) release "templates", they like to merge these two files.
Just create a new .cs file and paste the code and all should be good. It's the code basically saying "in the designer, we already have this stuff". (a good way to note this is the "partial" keyword located before your Form1 declaration)
More info:
The Code file will house all your own implementations. That is click events, methods you personally override, events you bind to, etc. This is the default file when you select "View Code" from either your solution explorer or the dialog itself. Within this file is a construct that calls a "hidden" method, (InitializeComponent) that if you right click and "Go to Definition" will bring you to the next file:
The Designer file is the IDE's generated file. This takes everything you do in the designer and stores it for you. That includes new controls, location and properties of the controls, and the IDisposable implementation. The idea is to keep the "meat an potatoes" out of the way while you worry only about implementation.
Yes it sounds like you've copied the entire code which includes many things already contained within your Form in a partial class. Either remove these or remove the partial class and partial class declaration from your Form to get rid of these errors
I went to the Microsoft site to see what you did. The site shows code for an entire "one file" solution. We've all agreed that Visual Studio creates multi-file solutions, so you're duplicating code.
I don't know if the current answers/comments have helped you get this sample code working, so I thought I'd add my share. I was able to get this sample working by doing the following:
First, where the sample code at the Microsoft site shows declarations for button, textbox, labels, and combobox, rather than attempting to copy that portion, I simply used the toolbox and dragged a button, the labels, the textbox, and the combobox from the toolbox to my form.
You'll probably want to arrange these to your liking.
This process created my form correctly with the appropriate objects on it. All I had to do was use the properties window for each object and rename them according to what they were named in the sample. For example, my new button was originally button1, but I renamed it to btnSearch just as it is named in the Microsoft sample.
I noticed that the Microsoft sample has an established event handler setup for the Form1_load() event. I created this same event in my form by clicking the form in the designer, clicking properties, clicking on the Events button in that properties, and double-clicking the "Load" event. This automatically generated the appropriate code.
In a similar way, I had to create the btnSearch_Click() event. I did this by simply double-clicking the button in the designer.
After that, all I had to do was manually copy and paste from the specific sections of the sample to my code -- fill in the Form1_Load() event with what was in the sample. Copy the DirSearch() method over. Fill in the btnSearch_Click() event. That was it.
I hope this helps solve the overall issue and gives you more insight into how you can avoid these problems in the future.
You have duplicated functionality in the classes, you have a file that was automatically generated with that functionality already in it.

Using a User Control from Another Project

I have built a user control in a project that I want to re-use in a separate project. I had seen similar attempts successfully accomplished by adding a reference to the original project and then registering the control via:
<%# Register Assembly="AssemblyName" Namespace="AssemblyName.Namespace" TagPrefix="xxx" %>
I did something similar, compiling the original project and referencing it in the subsequent one . The control has an asp:Repeater control within it. I then registered my control as above and placed the control on my page like so (the control has a string property named prop):
<xxx:ControlName ID="ControlId" runat="server" prop="21" />
However when I launch the control in the subsequent project, I get a null reference exception and the application chokes. Am I doing this the wrong way? Is it mandatory that I have an .ascx file in the subsequent project?
Any and all help is appreciated.
Thanks,
pbr
Just as a follow up to this:
I did some further research and learned that the mark-up language (materials on the .ascx page) do not get compiled into the assembly. Therefore you must have the .ascx file within the project that calls it if there is any markup associated with the control.
In the properties dialogue I set up a post-build event that copies the .ascx file over to the subsequent project Controls file. I reference it as I would any normal User Control and everything works fine.
Hope this helps someone out there with a similar issue,
pbr

Categories

Resources