This blog briefly introduces the concept of WebForm and the member information of the Page class. Therefore, this blog is just a simple description of the application before we get started, the following blog will start with the asp.net Server Control.
- WebForm
(1) It is too painful to directly use HttpHandler for each output webpage. Therefore, when HTML is generated, aspx (web form, WebForm) is created directly ).
(2) WebForm is divided into two files: aspx and aspx. cs and aspx are page templates, page description files, HTML content, and better combination with aspx, so programmers do not need to fill the templates as they did at the beginning, controls are defined in aspx, inline JavaScript, css is also written in aspx, and the server's C # code is defined in aspx. in cs, aspx controls the page appearance and cs controls the program logic. This "pre-aspx post-cs" method is called CodeBehind. Aspx is the template engine and does not need to find a third-party template engine.
(3) cs can call controls in aspx, aspx can also access fields and functions defined in cs, and compile Complex C # code, for and other C # code can be written to aspx.
1) write C # Code directly on the aspx page
1 <% for (int I = 0; I <10; I ++) {%> 2 3 how are you ?? 4 5 <% }%>
2) define attributes in cs:
1 public string MyName2 3 {4 5 get {return "";} 6 7} 8 9 <% = MyName %>
(4) use <% = UserName %> to output the expression value at the current position. Do not lose the equal sign "=", which is equivalent to calling Response. Write (UserName) at the current position );
(5) the code used is equivalent to calling the function and executing the code at this position. Note: In aspx, the cs member level must be protected or public, and private cannot be called.
- Relationship between aspx, cs, and dll
(1) execute the following code on the webForm page: Page_Load {}
1) Response. write (this. getType () + "<br/>"); // this indicates the object of the current class 2 3 2) Response. write (this. getType (). assembly. location + "<br/>"); // After editing, you can see where the Assembly is. 4 5) Response. write (this. getType (). baseType + "<br/>"); 6 7 4) Response. write (this. getType (). baseType. assembly. location + "<br/> ");
(2) It is found that the class name of the currently executed page is a class name such as ASP. Weborm_aspx, and the parent class is ASP. Weborm.
(3) Use Reflector to open the temporary dll and decompile the two classes. It is found that ASPNETTEST1.WebForm is used to compile aspx in. cs class, while ASPNETTEST1_aspx is a subclass inherited from ASPNETTEST1.WebForm1, ASP. the WebForm_aspx code is a class that dynamically generates webpage content based on the aspx content. In summary, aspx will generate a class that inherits from aspx. class in cs, view the code after decompilation, you can see that the compilation generates a common.. NET code. Because the code generated by aspx is a subclass of cs, you can see why "the membership level in aspx must be protected or public, rather than private ".
- Page members
(1) Request, Response, Server attribute, and simplified call to context. Request, context. Response, and context. Server.
(2) The AppRelativeVirtualPath attribute to obtain the path of the page relative to the application root path, for example :~ /Default. aspx.
(3) FindControl (strlID): locate the Control Based on the Control ID. Generally, write the Control ID in the Code and use the control ID. However, in some scenarios, if you want to use a template of Listview and other controls to write custom controls, you need to use FindControl to reference the Control. The FindControl return value is Control, which usually needs to be displayed and converted to the corresponding Control. TextBox txtBox = (TextBox) FindControl ("TextBox1"); txtBox. Text = "han Yinglong ";
Note: drag and drop a TextBox, Button, and double-click the Button to write the following code:
1 protected void button#click (object sender, EventArgs e) 2 3 {4 5 TextBox txt = FindControl ("TextBox1") as TextBox; 6 7 txt. text = "blog garden-Kencery"; 8 9}
(4) ResolveClientUrl (url) converts a virtual path to the access path of the client, for example, ResolveClientUrl ("~ /A/B. aspx ") the result is a/B. aspx, which is usually used to output HTML in the template of ListView and other controls, basically for VirtualPathutility. toAbsolute simplifies calling. Considering the relative path of the current page, the generated path is short and the most commonly used path.
(5) ResolveUrl (url) converts a virtual path to a path relative to the root directory of the website, for example, ResolveUrl ("~ /A/B. aspx ") is/WebSite/a/B. aspx. VirtualPathUtility. ToAbsolute is directly converted to a full path.
Note: Write the following code on the. aspx page, regardless of the current page
1 <% = ResolveClientUrl ("~ /Ashx/10th/Default. aspx ") %> <br/> 2 3 <% = ResolveUrl ("~ /Ashx/Article 10th/Default. aspx ") %>