creating the code behind file - asp.net

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.

Related

What is the preferred workaround when an .aspx file's code-behind file is missing?

In the source code for a VB ASP.NET website that I'm trying to get compiling, I've got a couple of "The file 'bla.aspx.vb' does not exist" errors; there is a corresponding .aspx file, but it's missing its companion .aspx.vb code-behind file.
The error msg displays because of this in the .aspx file:
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Bla.aspx.vb" Inherits="cms_ShowEscalatorRule" %>
To just get the project compiling (this is just for running the project locally, the code I change won't affect the production code), would it be preferable to:
0) Comment out that section entirely
1) Remove the "CodeFile="Bla.aspx.vb" portion
2) Add a code-behind file
If the last option is best, what needs to be in it for a minimal amount of code - just enough to prevent the "missing file" error?
It's rather macabre to me that there is no contextual menu item for .aspx files that allows a minimal corresponding .aspx.vb to be created.
You may have to have a file because the .aspx may have references (and you will have to recreate those in the code-behind) to items from the code-behind file.
The minimum that's needed is a class inheriting from Page:
public class Bla : System.Web.UI.Page
{
}
This - of course - is not mandatory - you may as well use the alternative approach and inline the code-behind in a server-side script block in the .aspx file. Former approach however is much neater.
G. Stoynev basically had what I needed, but since this is VB, I had to change it to:
Public Class Bla
Inherits System.Web.UI.Page
End Class
I have never used VB before, so I used this to make the translation.

The name does not exist in the current context

I have a masterpage in my asp.net 3.5 application and I have some controls and jquery stuff. I try to access the controls in codebehind and it says :
The name 'DrpStates' does not exist in the current context
Why it is not accessible in codebehind ?
When you create a code behind file, ASP.NET also automatically generates a designer file (which is right next to it). In that designer file all the controls are initialized and loaded. Sometimes (for reasons unknown) when you create a new control, it fails to re-initialize the designer file and you can't get access to the control in the code behind file.
Try doing this >
Delete the designer file (right click > delete)
Right click on the aspx file > Convert to Web Application
Should work now
It's probably part of the master page or parent page, try using FindControl method:
this.Page.FindControl("DrpStates");
There could be a problem with your .designer.cs file. Check if you have a designer file with the same name as your aspx (or ascx) file.
If you open the aspx file and switch between design view and html view and back it will prompt VS to check the controls and add any that are missing to the designer file.
Try right clicking on the aspx and select "Convert to Web Application".
You can also try deleting the .designer.cs file and then recreate an empty file with the same name.
reason : - When we create a code behind file, ASP.NET also automatically generates a designer file. In that designer file all the controls are initialized and loaded. Sometimes when we create a new control, it fails to re-initialize the designer file and you can't get access to the control in the code behind file.
There is a simple Solution to this situation.
Step1 : open the the yourfile.aspx.designer.cs file
you will find things like " protected global::System.Web.UI.WebControls.Label Label2; "
these are the initialized components in the sequence in which they were generated by you.
Step2: just copy and paste the following line repeatedly for every missing component that were not
recognized by the code behind : "global::System.Web.UI.WebControls."+Class of the component that you are missing + single space + id of the missing component.
Step3: save the file and voila all the components error disappear magically.

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" %>

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