<@ Page: Relationship between codebehind, inherits, and aspx
Codebehind
Specifies the name of the compiled file that contains the class associated with the page. This attribute cannot be used at runtime.
Note:
This attribute is provided to be compatible with previous versions of ASP. NET for code hiding. In ASP. NET 2.0, you should use the codefile attribute to specify the name of the source file, and use the inherits attribute to specify the fully qualified name of the class.
Codefile
Specifies the path of the code hidden file pointing to the page reference. This property can be used together with the inherits property to associate the hidden source file of code with the webpage. This attribute is only valid for compiled pages.
Inherits
Defines the code hidden class for page inheritance. It can be any class derived from the page class. This attribute is used with the codefile attribute, which contains the path to the source file of the Code hiding class. The inherits attribute is case-sensitive when C # is used as the page language time zone, while Visual Basic is not case-sensitive when used as the page language.
If the inherits attribute does not contain a namespace, ASP. NET checks whether the classname attribute contains a namespace. If it contains, ASP. NET tries to use the namespace of the classname attribute to load the class referenced in the inherits attribute. (This assumes that the inherits and classname attributes use the same namespace .)
For more information about the code hiding class, see ASP. NET webpage code model.
Classname
A string that specifies the name of the page class that will be dynamically compiled during request pages. This value can be any valid class name and can include a fully qualified class name. If the value of this attribute is not specified, the Class Name of the compiled page will use the default namespace ASP Based on the page name. If the value of the specified classname attribute does not have a complete namespace, ASP is used together with the specified class name to create a fully qualified class name.
For other pages, you can use the @ reference command to reference the class name assigned to the first page.
Note:
You can use the fully qualified Class Name Of The. ASPX page to reference the members of the associated. ASPX page class in the code hidden page class. However, if the source code protection is enabled for the pre-compilation, the code hidden page class and the. ASPX page class are not in the same assembly. Therefore, the classes referenced in the Code hiding file do not work. For more information about pre-compilation, see ASP. Net pre-compilation overview.
SRC
Specifies the path of the source file containing the Code that links to the page. In the source file of the link, you can choose to include the page programming logic in the class or the Code declaration block.
You can use the SRC property to link the generated provider to the page. For more information, see buildprovider class. In addition, in ASP. NET versions earlier than 2.0, the src attribute is another method to link a hidden code file to a page. In ASP. NET 2.0, the preferred way to link a hidden source file to a page is to use the inherits attribute to specify the class and use the codefile attribute to specify the path of the source file of the class.
Demo
Code
The inherits attribute is namespace (Liuxiang) + class name (inheritclass). inheritclass must inherit page
<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "default. aspx. cs" inherits = "Liuxiang. inheritclass" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: textbox id = "txtname" runat = "server"> </ASP: textbox>
<Asp: Label id = "labname" runat = "server"> </ASP: Label>
<Asp: button id = "btnname" runat = "server" text = "OK" onclick = "button#click"/>
</Div>
</Form>
</Body>
</Html>
The code in default. aspx. CS cannot reference any controls on the page in the code file because the inherits in <% @ page points to the class inheritclass.
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Namespace Liuxiang
{
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
}
}
}
The inheritclass code inherits system. Web. UI. Page and can directly reference controls.
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. UI. webcontrols;
Namespace Liuxiang
{
Public partial class inheritclass: system. Web. UI. Page
{
// Textbox txtname;
// Label labname;
// Button btnname;
Protected void button#click (Object sender, eventargs E)
{
Labname. Text = this.txt name. text;
}
}
}
This method has the advantage of encapsulating all the code on the page into a class file, and then shelling the DLL to effectively protect the code.
However, this method can be executed in the Asp.net web application! However, if it is a Web site, this method does not seem to work.
ASPnet: Make sure that the class defined in this Code File matches the "inherits" attribute and that the base class (such as page or usercontrol) of this class extension is correct.
This error!
Hope that expert will advise ~~~~~