asp.net server-side instruction include use and advantages introduction _ Practical Skills

Source: Internet
Author: User
Tags html tags

The service side of the asp.net includes the instruction simple point is a <!--#include file|virtual= "filename" –> such instructions, the term in MSDN is to insert the contents of the specified file into the asp.net file. Includes Web pages (. aspx files), user control files (. ascx files), and Global.asax files. Insert static file This basic function does not say, insert aspx, ascx, this function is quite strong, asax brother is a bit confused, this aside, today to say is this directive.

An awkward existence
Server-side include directives must be standard in web development technology. asp.net predecessor ASP has this directive, as the only one to refer to external documents, the introduction of the developer should have used; PHP include, require, almost used, used a section of PHP, this instruction commonly used can not be used JSP has not developed experience, search the next, there are also this include instructions, the use of the situation should also be very common. But the include in the ASP.net is almost in the forgotten corner of the ASP.net book, which is rarely mentioned. Therefore, without the experience of ASP Development Direct learning asp.net, may want to have no idea of its existence. Why??

One of the reasons for the embarrassment is one of the most important functions in WebForm: the user control. Why use include, because the referenced content is used in many places-reuse, such as headers, footers, Web site feature blocks. The exact user control is born to this, its inherently reusable feature, and powerful control event processes, as the MSDN says:
"Although you can still use #include tags (by putting common server-side code, controls, or HTML tags in files to be included in other Web pages) for code reuse, ASP.net's preferred method is to use Web user controls. User controls provide an object-oriented programming model and provide more functionality than server-side include files. ”
Microsoft did not want to see it, and then, the include was also put aside by developers, but, please do not forget that it exists.

Include detailed
Said is detailed, in fact, do not intend to explain here, see: Server-side including instruction syntax. Grammar, usage, example and so on, do not say here, everyone to see or test their own.

Include advantages
When it comes to the advantages, there must be a comparison of the objects, right, this object is the user control. What the user control is, does not explain.

One of the advantages: the reuse of static files.

User controls are powerful, but even as simple as static content is implemented as a user control, it must go through the lifecycle of the user control, which is worse than performance.

You may say, nowadays the website also has how many still can be pure static content, for example footer, those items certainly also is the website CMS backstage management, how possibly exists in the form of the static file, can only use the user control, and adds the outputcache to the user control, thus is guaranteed the flexibility, Performance can also be guaranteed, this is the most perfect.

Yes, first of all, different projects different requirements, certainly some time is to refer to static content, at this time to ensure the use of static content in the form of static, do not put into the user control. Second, the optimization of the website, whenever, there is always a view that static, for example, the top footer, we can generate a static footer file, when the CMS on these items have modified, regenerate the file, and then referenced by include, relative to the cache unpredictability and some uncontrollable , the advantages are more flexible than the user controls, and performance is better. Third, collaboration is for relatively large web sites, some of the page controls, but these data are other teams are responsible for, and some relatively stable content or change is not sensitive to the content, the generation of static files after the automatic distribution, and the reference to these files is also very common.

In fact, this may be the only advantage of the include.

The advantage of the second, can penetrate the encapsulation.

Reusable things are generally encapsulated. User control is a separate individual, it is hosted on the page, but itself is like a page on the realm, encountered within the Kingdom of processing, the page will handle the right to control their own to deal with, such as page Page_Load load, the control can only call the control provided by the load processing method, This is the encapsulation of the control, and only the properties and methods that it is willing to expose can be accessed by the page, which is the standard encapsulation, which is also determined by the ASP.net page life cycle. Therefore, if a control wants to use a variable of a hosted page, it should expose a property and then actively assign it to the page in the code.

In the same way, different mechanisms determine different behaviors. From MSDN:

The value assigned to the File or Virtual attribute must be enclosed in quotation marks (""). Processes the included file before executing any dynamic code.

The important sentence is to process the included file before executing any dynamic code. What do you mean by this sentence?

A layer of meaning that is included in the file is processed after the content will be added to the page, the dynamic code execution, the execution of the code can not distinguish whether this piece of content is a reference or own, that is, we can use to reuse the static file to add <% code%> or <%= Page variables%> Such code, they will be processed by the page as the corresponding content. This is what I call a "permeable package". This approach, of course, is much more flexible than a control exposes a property to a page's active assignment.

Two-layer meaning if the referenced aspx or ascx, then the page code executes after the referenced dynamic content executes, so that the process is not as good as the user control calls, and try not to use include references to dynamic pages.

So we should refer to static files only with include, but the static file can contain the service-side tag code.

Do not know, but Zennai ability is limited, let it be. Try to look at it, but it is certain that you can use the include bar for reuse as a static file. There may be other more appropriate usage scenarios and more advantages, and please be fully experienced to complement it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.