Thursday, January 25, 2007

Are you aware of Directives

Dear All here is some directive stuff enjoy
· @ Page Directive

This directive is used to assign page-specific attributes that are used by the web forms page parser and compiler. It can be placed anywhere in the page. A page can have only one @ Page directive.

· @ Register Directive

This directive is used to register a user or server custom control on the page.

1) To register a user control use:

<%@ Register TagPrefix = “Wrox” TagName=”CButton”
Src = “UserControls\Header.ascx” %>

To use the control:

Source file of a user control can have only one control.

2) To register a custom server control: These custom controls are compiled and contained within assemblies.

<%@ Register TagPrefix = “Wrox” Namespace = “WroxControls”
Assembly = “RatingMeter” %>

To use the control:

· @ Assembly Directive

This directive is used to refer an assembly for the page.

1) To use a compiled assembly: <%@ Assembly Name=”xyz” %>
2) To use a source file that will be compiled when the page is compiled: <%@ Assembly Src=”pathname” %>

This tag is usually not required, as any assembly that is in the ASP .Net application’s bin directory will automatically be compiled into the page. Use this when you refer any shared assembly or the assembly in another folder.

· @ OutputCache Directive

This directive is used to control how the page is cached. When caching for the page is enabled, the requested page is compiled for the first request and all the subsequent requests (even from another user) is served from that compiled cached page. The attributes for the directive are:

1) Duration: This attribute is used to control how long an item will stay in the cache before being invalidated. This is a mandatory attribute.
2) Location: This attribute specifies where the caching is stored. It can be Any, Client, Downstream, Server on None.
3) VaryByCustom: When the value of this attribute is set to “browser”, caching is varied by the type of browser.
4) VaryByParam: This attribute is used to vary the cache, based on the values of parameters passed to the server (i.e. QueryString parameter). Value for this attribute can be * (for all parameters) or none (do not vary on any parameters).

Output caching only works using ASP .Net on the windows 2000 and windows XP platform.

· The principle of code-behind is to create a class for the code, and inherit this class from the ASP .Net Page object. You can then create the ASP .Net page and use a page directive to inherit from the newly created class.

· Smart Navigation

This is an Internet Explorer specific feature, which requires IE 5 or above. Smart navigation has four feature as No more screen flash, Scroll position maintained, Element focus maintained, Last page in History maintained. Smart navigation only redraws the contents of the page that has been changed during a post-back. Entire page does not need to be redrawn. You can enable smart navigation at page level as: <%@ Page SmartNavigation=”true” %> or in Web.config as:

· User Control

A user control does not have a html tag. A source file of a user control has an extension .ascx. To use a user control on a page you have to first register it and then you can use it as a normal control.

When you have a property defined in a user control, you can use it like: (Say you have a property named ConnectionString)



Event for a user control must be handle in the user control page, not on the page hosting the user control. Because the user control’s event will not be passed to the hosting page, so the events will never be processed.

· Partial Page Caching

This is also known as fragment caching. It is available when you are using a user control on a page. You can compile and cache that user control first time the hosting page is requested, than on subsequent requests for the hosting page that cached user control is used to render the page. Enabling a caching for the user control is same as a normal page. Use @ OutputCache directive on the user control page.

· Adding the runat=”server” attribute to an HTML element causes that control to be compiled into the page, and executed on the server each time the page is requested. It means runat=”server” attribute makes a normal HTML element a server control. So, to be able to access the element’s properties, method, or events in the server-side code, you have to create it as a server control.

· The base class for the all HTML control is:
System.Web.UI.HTMLControls.HTMLControl

· Each server side event handler has two parameters. The first parameter is a reference to the object that raise the event and the second parameter is a type of EventArgs object that contains any additional information about the event.

· When using ASP .Net’s validation control you can check the page’s IsValid property to check whether the page passed the validation test or not. You can also use the CausesValidation=”false” property of a control (such as a cancel button), so that it can post back the page without occurring the validation process on the client side. Without this you won’t able to post back the page without passing all the validation check.