DOT NET 4.0 – Directives for Web Forms Pages – @ Register Directives

Associates aliases with namespaces and class names for concise notation in custom server control syntax.

<%@ Register tagprefix=”tagprefix Namespace=”namespace” Assembly=“assembly” %>

<%@ Register tagprefix=”tagprefix” Tagname=”tagname” Src=”pathname” %>

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 you are associating with tagprefix resides.

Note The assembly name does not include a file name extension.

Remarks

Including this directive in a page or user control allows you to layout custom server controls or user controls using declarative custom server control syntax.

Use this directive in one of the following situations.

  • You are declaratively adding a custom ASP.NET server control to a page or user control.
  • You are adding a declarative user control to a page or user control.

For declarative user controls, use the tagname and tagprefix attributes. These are always used together as a colon-separated pair (tagprefix:tagname) when you declare the control in the page.

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.

Important 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.ascx. The aliases are then used in custom server-control syntax within the form to insert an instance of each server control.

<%@ Register Tagprefix=”MyTag” Namespace=”MyCompany:MyNameSpace” Assembly=”MyAssembly” %>

<%@ Register Tagprefix=”Acme” Tagname=”AdRotator” Src=”AdRotator.ascx” %>

<HTML>

<body>

<form runat=”server”>

<MyTag:MyControl id=”Control1″ runat=”server” /><BR>

<Acme:AdRotator file=”myads.xml” runat=”server” />

</form>

</body>

</HTML>

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )

Advertisements

DOT NET 4.0 – Directives for Web Forms Pages – @ Reference [Visual Basic] Directives

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.

<%@ Reference page | control="pathtofile" %>

Attributes

Page

Web Form that ASP.NET should dynamically compile and link the current page against at runtime.

Control

User control that ASP.NET should dynamically compile and link the current page against at runtime.

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#]

<%@ Reference Control=”MyControl.ascx” %>

<script language=”C#” runat=server>

void Page_Load(Object sender, EventArgs e) {

// In @ Control directive in the MyControl.ascx file,

// you must include classname=”MyControl” for this to work.

MyControl myControl = (MyControl) Page.LoadControl(“MyControl.ascx”);

myControl.MyProperty = “Color”;

PlaceHolder.Controls.Add(myControl);

}

</script>

<html>

<body>

<asp:placeholder id=”PlaceHolder” runat=server/>

</body>

</html>

[Visual Basic]

<%@ Reference Control=”MyControl.ascx” %>

<script language=”VB” runat=server>

Sub Page_Load(sender As Object, e As EventArgs)

‘ In @ Control directive in the MyControl.ascx file,

‘ you must include classname=”MyControl” for this to work.

Dim myControl As MyControl = CType(Page.LoadControl(“MyControl.ascx”), MyControl)

myControl.MyProperty = “Color”

PlaceHolder.Controls.Add(myControl)

End Sub

</script>

<html>

<body>

<asp:placeholder id=”PlaceHolder” runat=server/>

</body>

</html>

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )

DOT NET 4.0 – Directives for Web Forms Pages – @ OutputCache Directives

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.

<%@ OutputCache Duration=”#ofseconds” Location=“Any | Client | Downstream | Server | None” VaryByControl=”controlname” VaryByCustom=”browser | customstringVaryByHeader=”headers” VaryByParam=”parametername” %>

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.

Important This attribute is not supported for @ OutputCache directives included in user controls.

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.

Important 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 ouput cache by all parameter values, set the attribute to *.

VaryByControl

A semicolon-separated list of strings used to vary the output cache. These strings represent fully qualified names of properties on a user control. When this attribute is used for a user control, the user control output is varied to the cache for each specified user control property.

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 header to private. For more information on all these subjects, see Caching Page Output.

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 : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )

DOT NET 4.0 – Directives for Web Forms Pages – @ Import Directives

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.

<%@ Import namespace="value" %>

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:

<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="Grocery" %>

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )

DOT NET 4.0 – Directives for Web Forms Pages – @ Implements [Visual Basic] Directives

Indicates that the current page or user control implements the specified .NET Framework interface.

<%@ Implements interface=”ValidInterfaceName” %>

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 <script> element in a code declaration block. You cannot use this directive to implement an interface in a code-behind file.

Example

The following example demonstrates a user control that includes an @ Implements directive to access to the properties and methods of the IPostBackEventHandler interface. To use this sample, include it in a .ascx file, then declare the user control in a Web Forms page (an .aspx file). For more information on how to include a user control in a Web Forms page, see the following topics: @ RegisterCustom Server Control Syntax, and Including a User Control in a Web Forms Page.

[C#]

<%@ Implements Interface=”System.Web.UI.IPostBackEventHandler” %>

<script language=”C#” runat=”server”>

//Defines Text property and Click event.

public String Text;

public event EventHandler Click;

//Evaluates data-binding expressions when page is loaded.

void Page_Load (Object sender, EventArgs e) {

this.DataBind();

}

//Implementation of the event

public virtual void RaisePostBackEvent(string eventArgument) {

OnClick(new EventArgs());

}

protected virtual void OnClick(EventArgs e) {

if (Click!= null) {

Click(this,e);

}

}

</script>

javascript:{0}”, Page.GetPostBackEventReference(this)) %>>

<asp:Label id=l1 Text='<%# Text%>’ runat=”server”/>

</a>

[Visual Basic]

<%@ Implements Interface=”System.Web.UI.IPostBackEventHandler” %>

<script language=”VB” runat=”server”>

‘Defines Text property and Click event

Public Property Text As String

Public Event Click As EventHandler

‘Evaluates data-binding expressions when page is loaded.

Sub Page_Load (Sender As Object, e As EventArgs) {

Me.DataBind()

End Sub

‘Implementation of the event

Public Sub RaisePostBackEvent(eventArgument As String) _

Implements IPostBackEventHandler.RaisePostBackEvent

OnClick(New EventArgs())

End Sub

Protected Overridable Sub OnClick(e As EventArgs) {

RaiseEvent Click(Me, e)

End Sub

</script>

javascript:{0}”, Page.GetPostBackEventReference(Me)) %>>

<asp:Label id=l1 Text='<%# Text%>’ runat=”server”/>

</a>

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )


DOT NET 4.0 – Directives for Web Forms Pages – @ Assembly Directives

Links an assembly to the current page during compilation, making all the assembly’s classes and interfaces available for use on the page.

<%@ Assembly Name="assemblyname" %>
<%@ Assembly Src="pathname" %>

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 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 <assembly> section of your application’s Web.config file:

<add assembly="*"/>

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 a user-defined assembly, MyAssembly, the second to link to a Visual Basic .NET source file, MySource.vb:

<%@ Assembly Name="MyAssembly" %>
<%@ Assembly Src="MySource.vb" %>

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )

 

DOT NET 4.0 – Directives for Web Forms Pages – @ Control Directives

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.

<%@ Control attribute="value" [attribute="value" … ] %>

Attributes

AutoEventWireup

Indicates whether the page’s events are autowired. true if event auto wiring 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 by automatically compiled dynamically 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. 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 only be used in user controls. User controls are defined in files with the .ascx extension. You can only include 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.

<%@ Control Language="VB" EnableViewState="false" %>

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )