How can I add a code-behind file to an aspx page? - asp.net

There are two pages in a legacy app to which I'm addding fuctionality.
One of the .aspx files, when the "Design" view is shown, sports an Events tab in the Properties pane:
The other, though, does not - it only shows Properties:
I need to add a custom method to this code; how can I create a corresponding .vb code-behind file where I can add this method?
UPDATE
I tried jackjop's suggestions, but F7 did nothing, and I do not seem to have the "Show All Files" glyph:
UPDATE 2
I tried adding this to the top of the .aspx file, mirroring what is in the aspx/aspx.vb pair that works as I want, but it didn't seem to make any difference:
<%# Page Language="VB" AutoEventWireup="true" CodeFile="custmaint_entry.aspx.vb" Inherits="pages_custmaint_entry" %>

When you create an ASPX file it also creates a code-behind file. You just can't see it.
From aspx file press F7 or click Show All Files button in Solution Explorer.
Notice that when Show All Files is not clicked, it doesn't show code behind files.
Or you can just simply right-click on aspx file on Solution Explorer and click View Code. If this also does not work, then you probably deleted the code behind files, I suggest you re-creating every file.

Related

how can I see the aspx.vb and aspx.designer.vb under my aspx file?

On this project you can see aspx.designer.vb and aspx.vb under the aspx file
but on my project I don't have it?? ,, what gives??
I can view the vb code by right click then click view code.
How can I make the aspx.vb and the designer file to show under the aspx file?
thankx
One more picture. I can see the designer file and VB file in file explorer
just press Show All Files on Solution Expolorer ToolBar
It Could be beacause:
1) You need to press the button (top of the explore panel)
Nested related file
2) The file is created with the code in the same aspx file. When you create a new aspx file you could check de option
place code in a separate file
3) For some reason your codebehind isn't included in the project. If this is the case include it.

Visual Studio creation of WebForms vs Blank Files

When trying to write the codebehind file for its associates aspx page a colleague and I were stumped as to why the Controls within file.aspx were not appearing in the codecompletion, nor were they compiling without error.
When i created the file.aspx and file.aspx.cs I had created these items as blank files, the file.aspx.cs build action was set to Compile while the file.aspx Build Action was Content.
We created a new "webform" item that automatically generated the SecondFile.aspx and the SecondFile.aspx.cs file and associated them. We copied the HTML and code from file to SecondFile and the code completion worked, and the codeBehind compiled correctly.
Why?
You have not included enough information, but I think you might have forgot to put the page declaration at the top of your .aspx file
<%# Page Language="C#" CodeFile="file.aspx.cs" Inherits="my_ascx_class_name" %>

creating the code behind file

First i created the TimeSheet.aspx file,then added code files names TimeSheet.aspx.cs file.
created a TimeSheet class and code some code in that ,later in the #page directive added the codebehind attibute with value "TimeSheet.aspx.cs" and inherits attribute with value TimeSheet.
Now i want to make this code behind file to show up as a sub-branch of TimeSheetp.aspx.Just like a designer file.
Like
TimeSheet.aspx
|----TimeSheet.aspx.cs
how will i do that.please help me
At the top of the Solution Explorer, toggle Nest Related Files.
If you did not let VS create the file for you when you first make the file, you just need to create a new file (class file, '.vb' or '.cs' extension) in the same directory as the file you wish to add the code behind file to with that file name as the first part of the new file name. Example, default.aspx's code behind file that you create manually will be called "default.aspx.cs". Then in your original file, make sure you add a page directive for the newly created file: <% #Page Language="VB" Explicit="True" Codebehind="default.aspx.vb" %>
The Page directive should be configured automatically and the code-behind created when you create your page in VS.
If you are using web forms, you shouldn't have to create the .cs file; it should create it for you. Right click the aspx and do view code to see the code file. if you are in an MVC project, there isn't any code-behind, but a controller file. And so creating the cs file doesn't do anything.

codebehind cannot reference the page controls

I have controls in ascx file but i can't see them in intellisense in .cs file.It was working nice before.
I can see the control names in designer.cs file.
I have deleted the Asp.net temp files in AppData folder but still not working.The other user control files in the app can reference coerrectly to it's page controls. What is the problem here ?
I use VS2008.
Look at the top line of the .ascx page and check out the value of Inherits= and make sure that is has the right namespace.class appropriate for your codebehind. For example if your namespace is ProjectNamespace and your control class is MyControl then it should be ProjectNamespace.MyControl. This can get out of synch if you renamed the ascx file, etc and cause this type of problem.
This happens to me every time I copy a user control from one project to another. The connection between the ascx and the code-behind breaks.
This solution is tedious but it gets around the problem:
Create a new User Control. Visual Studio will correctly connect the ascx file to ascx.vb file.
Copy the ascx and vb code from your original control into the new one.
Delete your original control.
You now have a working control, but it has a different name.
If it is important to retain the name of the original control, repeat the whole process again and copy the second control to a third one with the correct name.
Write to Microsoft and ask them to stop adding bells and whistles we don't need and fix the basic stuff!
I just figured this out for my situation: in the Page parameters of the .aspx file, the 'Codefile' parameter was pointing to the file name: 'LabEdit.aspx.cs'. It should have been pointing to the path: "~/WOPages/LabEdit.aspx.cs". I know I didn't move the codefile or the page file, so this is maybe a problem with VS2008
I have solved the problem, I have uninstalled and reinstalled VS2008 and it is solved.

User control IDs not Being Recognized in Code-behind after Port to WAP

I copied over all my files from my Web Site Project to the root of my new WAP project. Now none of my user controls IDs in my .aspx pages are being recognized in the related code-behind pages and not sure why.
For example, our Master Page has the following user control referenced:
<%# Register Src="Navbar.ascx" TagName="Navbar" TagPrefix="mc" %>
and in the Master Page it's being used:
<td id="tdNavBar"><mc:Navbar runat=server id="navbar"></mc:Navbar></td>
but now, some of the code in my Master.cs that was referencing navbar no longer knows about it.
If they're not recognised in your code behind file, double check that they have been declared in the .designer.cs file. If they are not there, try manually adding them and see if that works correctly.
You might also want to try manually creating a new user control using the File --> New Item option to check that it isn't to do with when you copy and pasted the items in (i.e. they could have the wrong namespace, etc).

Resources