aspx.designer.cs how does it work? - asp.net

I'm a really beginner so my question may be appear ridiculous.. But, i wonder how the files .aspx.designer.cs works..
It's the first time i work with a solution containing files .aspx.designer.cs for each pages. So i understand it's declaration of controls used in the .aspx for code-behind..
Here is my questions:
Why sometimes solutions doen't have .aspx.designer.cs files? (is the files hidden or doesn't exists?)
I often have problems with this files, they don't Automatically recreate declarations of controls when i add some in the .aspx code, what am i doing wrong?

The .aspx.designer.xx files are the bridge for the ASP.NET webforms code-behind files and the .aspx markup files. Any server control existing on the ,aspx markup page is represented here. Most important are the name and type of the server control.
This, in part, allows Visual Studio to give the user IntelliSense in the code-behind page for server controls created at design-time.
How they work: Visual Studio will generate, or keep in sync, a protected member in the .designer file when you add/remove a server control from the designer.
protected global::System.Web.UI.WebControls.DropDownList DropDownList1;
Notice that .designer files create a partial class. This provides the linkage to the code-behind file. That's how Intellisense gets the hook between the .aspx and the code-behind.
You can regenerate your designer file: web.archive.org for undermyhat.org

Visual Studio has two approaches for creating websites: Web Site Projects and Web Application Projects. (OK, OK, three if you add MVC).
Only Web Application Projects have designer.cs files.
Web Site Projects don't have them.
The Web Application Project type was added in Visual Studio 2003.

As p.campbell pointed-out, the .designer.cs file links the .aspx file to its .aspx.cs CodeBehind file. Without the .designer.cs file, every .aspx page control in the .aspx.cs CodeBehind file will return the error, "does not exist in the current context". The linkage in .designer.cs is done based on the "Inherits" property of the # Page directive in the aspx file together with the namespace and class of the .aspx.cs CodeBehind file. The final segment of the "Inherits" property must match the class defined in both the CodeBehind file and the .designer.cs file, and the segments prior to it must match the namespace of the .designer.cs and CodeBehind files.
Example:
myfile.aspx
<%# Page Language="C#"
AutoEventWireup="true"
CodeBehind="myfile.aspx.cs"
Inherits="my.namespace.dot.classname" %>
myfile.aspx.cs
namespace my.namespace.dot {
public partial class classname : Page { ... }
}
Note: the CodeBehind file class must inherit from the Page class, or some derivative thereof.
myfile.designer.aspx.cs
namespace my.namespace.dot {
public partial class classname { ... }
}
Note: the .designer.cs class doesn't care about inheritance, just that the class name matches the CodeBehind and .aspx files.
You can regenerate a lost .designer file like this (w3cgeek.com "Regenerate designer.cs"):
Create a new, blank file in same dir as your .aspx and .aspx.cs files named "myfile.aspx.designer.cs" where "myfile" is the name of the .aspx and .aspx.cs files that you want to link.
Add a namespace with an empty class to the new file, and ensure their names match the namespace and class specified in the .aspx and .aspx.cs files which you are linking.
Save the .designer.cs file, make any change to the .aspx file (e.g., adding a space), and save the .aspx file.
Visual Studio should auto-populate the .designer.cs file with all the necessary code to link your .aspx and CodeBehind files. The "does not exist in the current context" errors should now be gone!
EDIT: I added the .designer.cs instructions because the link is dead which was originally posted by p.campbell.

Related

Pages are calling the wrong codebehind?

I have a project I'm working on in a Visual Studio 2003 and SQL Server 2005 environment. Lately I've been having some trouble with the codebehind files for my ASPX web pages. Some pages are referring to codebehind from other pages.
Recently, for example, I copied a page from one of my projects to use it as a starting point (it is for a form with multiple pages), After renaming it, the codebehind and other files followed suit. However after modifying the page to what I needed, I found out that it was still referencing the old codebehind (making references to controls that are no longer there).
I checked the aspx code, and the reference is correctly pointing to the new codebehind. I've tried building and re-building the project several times, resetting the IIS, deleting temp files and modifying the web config. No dice.
If you copy a page in Visual Studio and rename it, it will automatically change the Codebehind attribute in the <%# Page declaration on the .aspx markup.
But what it won't do is change the name of the class in the codebehind file, or the class that the Inherits attribute on the declaration points to.
Firstly, change the Inherits to your new class name - this should automatically change the designer file to use the same class name.
Then change the class definition in the code-behind file.

Does visual studio includes only aspx.cs files in dll which are having same namespace of project?

I am using visual studio 2012. I have created a directory named "Handler" inside my project and added an aspx file along with .cs file (both downloaded from a third party tool) to it. All code is working fine.
Now, I have published project to another directory. In published directory I have marked that the in side "Handler" folder aspx page is there but not the .cs page.
Now, when ever I am requesting the aspx page inside "Handler", I am getting error 404 xxxx.aspx page not found. Now, if I am coping the path and hit the url for that page then I am getting error that xxxx.aspx.cs file not found.
As per my best knowledge, whenever we build the project, all the code will build to dll in bin folder. Then why this file is required .cs file?
Thanks.
This will depend on the attributes of the #Page directive of your aspx page
I guess your third party page #page directive holds a CodeFile attribute.
CodeFile attribute states that, even though the .aspx.cs gets compiled (let's say it just gets checked) in Visual Studio, the result of the compilation will not be put in the dll resulting of the build.
The .aspx.cs will be compiled, along with the .aspx upon first invocation.
This allows to edit your deployed aspx.cs and have the result available immediately (which can be seen as handy, or risky)
As per my best knowledge, whenever we build the project, all the code
will build to dll in bin folder. Then why this file is required .cs
file?
This is the case when being in the default case, having a CodeBehind attribute in the page directive,instead of a CodeFile attribute
See CodeBehind and CodeFile attributes in documentation for #Page element
Note that you can use CodeFile attribute in Web Application Projects. I do it all the time.

How can I remove ASP.NET Designer.cs files?

I've worked on VS projects before where there is no .designer.cs files.
Now I started a new project on a different computer and I can't get rid of designer.cs files. It's really annoying me. Do I really need it, how can I remove it? There must be a setting somewhere.
YES! You can remove them......here is how.....
HOW TO DELETE DESIGNER.CS PAGES FROM YOUR WEB APPLICATION
After great torture and much testing seeking a way to avoid designer.cs pages in Visual Studio (v. 2015 and earlier), I finally found a work around for this. If anyone is stuck with designer.cs pages in a Web Application Project in Visual Studio this solution will allow you to erase all application compile errors quickly then delete the designer.cs pages completely from your project.
First understand the following:
Web Application Projects use designer.cs pages (partial classes) auto-generated by Visual Studio, and which are tied to the design tools built into Web Applications which the web app model sustains. I call it web sites for dummies. I could find no setting or way to turn the creation of partial classes and designer.cs pages off, as they are often unnecessary code tied more to the IDE than the functioning of the application. All partial classes get compiled into one class anyway. Web Apps also get pre-compiled or built ahead of time in general by design, and those dll's gets pushed into bin folders.
Web Site Projects do not use designer.cs files and are less tied to the IDE. They allow for a cleaner coding of class structures. They also use partial classes. But Web Sites Projects generally get compiled at runtime.
SOLUTION - How to Remove "...designer.cs" errors and files from a Web Application Project in Visual Studio.
Unless you want to convert your web project from an Application to a Web Site model in Visual Studio, this solution below allows you to run your project as is, yet like a Web Site, where you can move most or all the designer.cs partial class control references from that file into your main web page partial class file. It also removes all errors AND does not interfere with the Visual Studio recreating those designer files should other developers be sharing them and adding controls to pages and forget to use this solution on specific pages.
In the front-end .aspx pages, in the #PAGE directive at the top, change the "CodeBehind" to "CodeFile". Make sure it still references the same .cs code or class file.
Add the "CodeFileBaseClass" attribute to the same #Page directive in your web page and have it access the fully qualified path to the same .cs above with any namespaces in the path.
Make sure you use the "Inherit" attribute with the same path as the "CodeFileBaseClass".
You should have the following for every web page in your Web Application with these attributes formatted as such:
<%# Page Language="c#" CodeFile="index.aspx.cs" CodeFileBaseClass="YourNameSpace.index"
Inherits="YourNameSpace.index" %>
Now go into your designer.cs file for the page and copy any control references from the partial class in your designer file into the partial class of your main .cs file for the web page. If you have lost your designer.cs files, just add in any web controls as field references as fields that the compiler says are missing. After you do that DELETE the designer.cs file. You don't need them and have complete control over your web page controls using the main .cs file and its partial class.
Below is what I had in the designer.cs file before deleting it. Below it is the main index.aspx.cs file for my project after I added the designers.cs control reference as a field in its partial class. I then deleted the designer.cs file and its code completely from my project:
namespace YourNameSpace
{
public partial class WebForm1
{
/// <summary>
/// form1 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.Literal Message1;
}
}
In my index.aspx.cs file below you can see where I pasted the last "Message1" reference line from the designer above into its partial class at the top. You can see how I was then able to access the web page Literal control, Message1, in my Page_Load event and modify the text. That shows it was referenced correctly now, compiled, and worked, where before such references failed if the designer.cs file was missing or its partial class failed:
namespace YourNameSpace
{
public partial class index : System.Web.UI.Page
{
protected global::System.Web.UI.WebControls.Literal Message1;
protected void Page_Load(object sender, EventArgs e)
{
this.Message1.Text = "hello world";
}
}
}
The key to fixing this is the CodeFileBaseClass and CodeFile attributes in the #Page directive in the main web page. This prevents new member references and controls in the web page from being automatically generated by the Visual Studio IE and stuck as stubs in designer page partial classes (which to me is not helpful when you want full control over those fields in a single convenient area).
Note: If you later regenerate the designer.cs files again for your project by selecting your project in VS, then in the top menu choosing Project > "Convert to web Application" those designer.cs files will get recreated, BUT Visual Studio will ONLY place references to missing web controls in them that are NOT now added to your main .cs partial class page. In other words, any references I copied and added above to my index.aspx.cs partial class file would not be recreated in the new designer.cs file. So there is no conflict with the Web Application model you are using. When you see those designer files, you can now have developers safely copy them into your main code behind .cs files and safely delete the designers if you like.
You're dealing with a web application rather than a website (clarification)
Yes, in the context of a web application, you do need them.
The real "answer" is for MS to simply allow you to not use them.

Where can I put my source files?

I am developing a web form using Visual Web Developer
Currently I have class source files in the same directory as the web form, or in the App_Code folder. I'd like to organise the files into folders within the web form folder, but I can't find a way of adding a reference to the folders, and they don't seem to be being picked up automatically.
These are files that are under constant development.
What is the asp.net/c#'s equivalent concept to c++'s #include?
This is a Web Site rather than a Web Application
I'd suggest taking these out into a separate class library project and you can then reference this DLL in your web project. You would then add a 'using' statement at the top of your web form code to include this reference.
In a C# file (foo.cs), you would use:
using MyProjectsDefaultNamespace.Folder1.Folder2
In an aspx or ascx file, you would use:
<%# Import Namespace="MyProjectsDefaultNamespace.Folder1.Folder2" %>
Never really thought of doing this, but i guess i would do this as follows.
A folder represents a namespace. So where it says inherits="Project.PageName" in your PageName.aspx file, it should state inherits="Project.Folder.Folder.PageName". You also have to change the namespace in your PageName.aspx.designer.cs and PageName.aspx.cs files.
EDIT:
For ASP.Net website simply adjust your CodeFile attribute:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Folder/Folder/Default.aspx.cs" Inherits="_Default" %>
Its not clear if you mean the codebehind .aspx.cs file or a standalone .cs class file.
You can't have .cs files loose in your main folder. It has to be in the app_code folder (or in a class library if you were doing a WAP).
Your .aspx.cs files are paired up with your .aspx file. I wouldn't recommend trying to move these files away if thats what you are trying to do.
The top level namespace that contains _Default or any code that doesnt appear to have a namespace is ASP. This is generally hidden within Visual Studio. So its true name is ASP._Default
And the answer is, obtained from looking into the other answers thanks:
Organise the files into folders within the App_Code folder, they will automatically be included.
Right click on project in solution explore
Go to Add> Add ASP.NET Folder > App_Code
Then add your files/folder structure there
BUT, be warned of what you put in there. Please refer to:
http://vishaljoshi.blogspot.com/2009/07/appcode-folder-doesnt-work-with-web.html
In the properties of the source file(s), you might want to set the "Build Action" property to "Compile"

asp.net web sites and default namespaces and LINQ Datacontext part 2

Let me try to ask this question from a different angle.
I noticed that everytime an aspx page gets rendered in the browser using the "web site" model, a random assembly gets created 'on-the-fly' in the Temporary ASP.NET files. Analyzing the assembly in Reflector shows that the class created for any given .aspx file is under the "ASP" namespace.
So, starting with a empty "Temporary ASP.NET Files" directory, I opened my ASP.NET "website" in VS2008, and launched the default page. Immediately I observed that a random directory was generated inside that folder. Working my way down the path, I found 2 DLLs created: App_Code.1lywsqqz.dll, and App_Web_iohekame.dll. I assume that all the .aspx pages in the website get compiled into App_Web dll and everything in App_Code folder gets compiled into App_Code.dll.
So if my App_Code C#/VB.net files are under the "ASP" namespace, and my App_Web files are created under the "ASP" namespace, how come I still get an error "Could not load type 'ASP.NothwindDataContext'?
Somebody said "you don't need namespaces in the App_Code folder", but I tried it without and still get "Could not load type 'NorthwindDataContext'".
So what's going on between the App_Code folder, the rest of the site, and namespaces?
EDIT:
Here's my LinqDataSource in my .aspx file:
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="NothwindDataContext" EnableUpdate="True"
TableName="Categories">
</asp:LinqDataSource>
Neither "NorthwindDataContext", nor "ASP.NorthwindDataContext" works.
Types in App_Code C# source files, just like any C# file, will not be put in a specific namespace unless specifically declared by namespace Name {...} around it. So a class MyClass declared in App_Code will have the fully qualified type name MyClass. Just that.
You can reference it in Web.config as: "MyClass, App_Code".
Side note: When you are using a DBML in App_Code, the namespace of generated classes are defined in that file (look at the properties window when DBML file is open). If you specify a namespace in that file, naturally, your classes will be defined in that namespace. Note that this does not contradict with what I said above. The thing is, the LINQ data context generator processes the file and defines the classes in the specific namespace.

Resources