Defines user-control (.ascx file) specific attributes that are used by the ASP.NET page parser and compiler. This directive can be used only with user controls.
Attributes
AutoEventWireup
Indicates whether the page's events are autowired. true if event autowiring is enabled; otherwise, false. The default is true. For more information, see Web Server Control Event Model.
ClassName
Specifies the class name for the page that will be dynamically compiled automatically when the page is requested. This value can be any valid class name but should not include a namespace.
CompilerOptions
A string containing compiler options used to compile the user control. In C# and Visual Basic .NET, this is a sequence of compiler command-line switches.
Debug
Indicates whether the page should be compiled with debug symbols. true if the page should be compiled with debug symbols; otherwise, false.
Description
Provides a text description of the page. Supports any string description.
EnableViewState
Indicates whether view state for the user control is maintained across page requests. true if view state is maintained; otherwise, false. The default is true.
Explicit
Determines whether the page is compiled using the Visual Basic Option Explicit mode. true indicates that the Visual Basic explicit compile option is enabled and that all variables must be declared using a Dim, Private, Public, or ReDim statement; otherwise, false. The default is false.
Note This attribute is ignored by languages other than Visual Basic .NET. Also, this option is set to true in the Machine.config configuration file. For more information, see Machine Configuration Files.
Inherits
Defines a code-behind class for the user control to inherit. This can be any class derived from the UserControl class. For information about code-behind classes, see Web Forms Code Model.
Language
Specifies the language used when compiling all inline-rendering ( and ) and server-side script blocks within the user control. Values can represent any .NET-supported language, including Visual Basic, C#, or JScript .NET.
Strict
Indicates that the page should be compiled using the Visual Basic Option Strict mode. true if Option Strict is enabled; otherwise, false. The default is false.
Note This attribute is ignored by languages other than Visual Basic .NET.
Src
Specifies the source file name of the code-behind class to dynamically compile when the user control is requested. You can choose to include programming logic for your page either in a code-behind class or in a code declaration block in the .ascx file.
Note RAD designers, such as Visual Studio .NET, do not use this attribute. Instead, they precompile code-behind classes and then use the Inherits attribute.
WarningLevel
Indicates the compiler warning level at which you want the compiler to abort compilation for the user control. Possible values are 0 through 4. For more information, see the CompilerParameters.WarningLevel Property property.
Remarks
This directive can be used only in user controls. User controls are defined in files with the .ascx extension. You can include only one @ Control directive per .ascx file.
Example
The following code instructs the ASP.NET page compiler to use Visual Basic as the inline code language and disables saving view state across HTTP requests using the EnableViewState attribute.
@Import:
Explicitly imports a namespace into a page, making all classes and interfaces of the imported namespace available to the page. The imported namespace can be part of the .NET Framework class library or a user-defined namespace.
Attributes
namespace
The name of the namespace to import to the page. This can include any of the namespaces included in the .NET Framework or a custom namespace.
Remarks
The @ Import directive cannot have more than one namespace attribute. To import multiple namespaces, use multiple @ Import directives.
The following namespaces are automatically imported into all pages:
System
System.Collections
System.Collections.Specialized
System.Configuration
System.IO
System.Text
System.Text.RegularExpressions
System.Web
System.Web.Caching
System.Web.Security
System.Web.SessionState
System.Web.UI
System.Web.UI.HtmlControls
System.Web.UI.WebControls
Example
The following code imports the .NET Framework base-class namespace System.Net and the user-defined namespace Grocery:
@ Assembly
Links an assembly to the current page during compilation, making all the assembly's classes and interfaces available for use on the page.
Attributes
Name
A string that represents the name of the assembly to link to the page.
Note The assembly name does not include a file name extension.
Src
The path to a source file to dynamically compile and link against.
Note You cannot include a Name and a Src attribute in the same @ Assembly directive. If you want to use both, you must include more than one directive on the page.
Remarks
The compiler references the assembly at compile time, allowing early binding. Once compilation of the requested page is complete, the assembly is loaded into the application domain, allowing late binding.
Assemblies that reside in your Web application's \bin directory are automatically linked to pages within that application. Such assemblies do not require the @ Assembly directive. You can disable this functionality by removing the following line from the
Note You cannot include the path to an assembly in an @ Assembly directive.
As an alternative to using the @ Assembly directive, you can use the Web.config file to link assemblies across an entire application. For more information about the Web.config file and configuring your application, see ASP.NET Configuration.
Example
The following code fragment uses two @ Assembly directives, the first to link to MyAssembly, a user-defined assembly, the second to MySource.vb, a Visual Basic .NET source file.
@ Register
Associates aliases with namespaces and class names for concise notation in custom server control syntax.
Attributes
tagprefix
An alias to associate with a namespace.
tagname
An alias to associate with a class.
Namespace
The namespace to associate with tagprefix.
Src
The location (relative or absolute) of the declarative user control file to associate with the tagprefix:tagname pair.
Assembly
The assembly in which the namespace that you are associating with tagprefix resides.
Note The assembly name does not include a file name extension.
Remarks
Including the @ Register directive in a page or user control allows you to lay out custom server controls or user controls using declarative custom server control syntax.
Use the @ Register directive in the following situations.
To declaratively add a custom ASP.NET server control to a page or user control.
To add a declarative user control to a page or user control.
For declarative user controls, use the tagname, tagprefix, and src attributes. The first two are always used together as a colon-separated pair (tagprefix:tagname) when you declare the control in the page. The src attribute value can be either a relative or absolute path to the user control source file from your application's root directory. For ease of use, it is recommended you use a relative path. For example, assume you store all of your application's user control files in a \usercontrol directory that is a sub-directory of your application root. To include the user control found in a usercontrol1.ascx file, include the following in the @ Register directive:
Src="~\usercontrol\usercontrol1.ascx"
The tilde (~) character represents the root directory of the application.
Note If your user control is in the same directory as the page that contains it, the src attribute value should be the name and extension of the .ascx file.
When including custom server controls that you have compiled into a .dll for use with your application, use the tagprefix with the Assembly and Namespace attributes. If you do not include the Namespace attribute, or if you assign an empty string to it, a parser error will occur.
CAUTION When you develop a custom server control, you must include it in a namespace. If you do not, it will not be accessible from an ASP.NET page. For more information about developing custom ASP.NET server controls, see Developing a Simple ASP.NET Server Control.
Example
The following code fragment uses @ Register directives to declare tagprefix and tagname aliases for a server control and a user control. The first directive declares the MyTag alias as a tag prefix for all controls residing in the MyCompany:MyNameSpace namespace. The second directive declares Acme:AdRotator as a tagprefix:tagname pair for the user control in the file Adrotator.acscx. The aliases are then used in custom server-control syntax within the form to insert an instance of each server control.
@Implements
Indicates that the current page or user control implements the specified .NET Framework interface.
Attributes
interface
The interface to be implemented on the page or user control.
Remarks
When you implement an interface in a Web Forms page, you can declare its events, methods, and properties between opening and closing tags of a
>
[Visual Basic]
>
@OutputCache
Declaratively controls the output caching policies of an ASP.NET page or a user control contained in a page. For more information about the output cache, see ASP.NET Caching Features.
Attributes
Duration
The time, in seconds, that the page or user control is cached. Setting this attribute on a page or user control establishes an expiration policy for HTTP responses from the object and will automatically cache the page or user control output.
Note This attribute is required. If you do not include it, a parser error occurs.
Location
One of the OutputCacheLocation enumeration values. The default is Any.
CAUTION This attribute is not supported for @ OutputCache directives included in user controls (.ascx files).
Shared
A Boolean value that determines whether user control output can be shared with multiple pages. The default is false. For more information, see the Remarks section.
Note This attribute is not supported for @ OutputCache directives included in ASP.NET pages (.aspx files).
VaryByCustom
Any text that represents custom output caching requirements. If this attribute is given a value of browser, the cache is varied by browser name and major version information. If a custom string is entered, you must override the HttpApplication.GetVaryByCustomString method in your application's Global.asax file.
VaryByHeader
A semicolon-separated list of HTTP headers used to vary the output cache. When this attribute is set to multiple headers, the output cache contains a different version of the requested document for each specified header.
Note Setting the VaryByHeader attribute enables caching items in all HTTP 1.1 caches, not just the ASP.NET cache. This attribute is not supported for @ OutputCache directives in user controls.
VaryByParam
A semicolon-separated list of strings used to vary the output cache. By default, these strings correspond to a query string value sent with GET method attributes, or a parameter sent using the POST method. When this attribute is set to multiple parameters, the output cache contains a different version of the requested document for each specified parameter. Possible values include none, *, and any valid query string or POST parameter name.
CAUTION This attribute is required when you output cache ASP.NET pages. It is required for user controls as well unless you have included a VaryByControl attribute in the control's @ OutputCache directive. A parser error occurs if you fail to include it. If you do not want to specify a parameter to vary cached content, set the value to none. If you want to vary the output cache by all parameter values, set the attribute to *.
VaryByControl
A semicolon-separated list of strings used to vary a user control's output cache. These strings represent the ID property values of ASP.NET server controls declared in the user control. For more information, see Caching Portions of an ASP.NET Page.
Note This attribute is required in a user control @ OutputCache directive unless you have included a VaryByParam attribute. This attribute is not supported for @ OutputCache directives in ASP.NET pages.
Remarks
Setting values for the page output cache is the same as manipulating the HttpCachePolicy.SetExpires and HttpCachePolicy.SetCacheability methods through the HttpResponse.Cache property. Setting the VaryByParam attribute when creating a user control implements partial-page caching for that control.
If a Web Forms page requires authorization to be viewed by a user, the output cache sets the Cache-Control HTTP header to private. For more information on all these subjects, see Caching ASP.NET Pages.
If you set the Shared attribute to true, cached user control output can be accessed by multiple Web Forms pages. If you do not set it to true, the default behavior is to cache one version of user control output for each page that contains that user control. You can potentially save a significant amount of memory by enabling the Shared attribute. For more information, see Caching Portions of an ASP.NET Page.
Example
The following example demonstrates how you can set the duration that a page or user control is output cached.
The next example demonstrates how you can instruct the output cache to cache a page or user control by the location and count form parameters from a form's POST or from a query string. Each HTTP request that arrives with a different location or count parameter (or both) is cached for ten seconds. Any subsequent requests with the same parameter values are satisfied from the cache until the entry expires.
@ Reference
Declaratively indicates that another user control or page source file should be dynamically compiled and linked against the page in which this directive is declared.
Attributes
Page
The Web Forms page that ASP.NET should dynamically compile and link the current page against at run time.
Control
User control that ASP.NET should dynamically compile and link the current page against at run time.
Remarks
Using this directive allows you to dynamically compile a user control and add it to the ControlCollection object, accessed through the Controls property, for the page or server control. This allows you to cast the returned type after you have called the LoadControl method (inherited by the page from the TemplateControl class).
Example
The following example demonstrates using this directive to link a user control, MyControl.ascx, and load it to a containing page using the LoadControl method. When it is loaded to the page, the user control's MyProperty value is set, and the user control is added to a PlaceHolder server control's ControlCollection object through the Controls property.
[C#]
[Visual Basic]
Print this post
No comments:
Post a Comment