ASP.NET directives are special instructions to CLR about the behaviour and operations of the webforms.Every ASP.NET page contains directives.In other words,these directives are commands that the compiler uses when the page is compiled.We can control the behaviour of page by using directives.It doesn't matter that the page uses code-behind model or the inline-coding model,we can use directives in our application.
Directive is opened with .The format of a directive is as follows :
<%@[Directive] Attribute=value %>
we can add more than one attribute to directive statements as follows:
<%@[Directive] Attribute=value Attribute=value %>
There are 11 directives in ASP.NET 2.0.
Page - when the page is complied the @Page directive enables us to use attributesand values for an .aspx page.
Control - Enable us to use with user controls(.ascx).
Register - Registers one .ascx file to web-page[.aspx].
OutputCache - Used for Client-side caching.
Import - Imports a Namespace[Package] into the page.
Implements - Implements one Interface to web-form[.aspx/.ascx].
Reference - Used for Page To Page Data Transfer.
Assembly - Includes one Assembly[.dll] to the application.
PreviousPagetype - Enables the page in an application to work with postback from other Page.Master - Defines one Master[.master] Page.MasterType - Uses a class name to a page within specified master page.
Tips:
(1.)Although page still compiles if we put these directives at a different place in the page but it is good programming practice to put these directives at the top of our page or control.
(2.)@Page and @Control directives can not be used at the same time in .aspx page.
Introduction:
This tutorial illustrates how to Implement Fragment Caching, this caching is conceptually same as Page caching. Here we will create a User Control file [.ascx] which will show System Date & Time in a Label control and embed it in a aspx.page that will also show system's Current Date & Time.
We will use Directive in the User control file and Directive in our Main Page to embed User Control file below Directive.
Same as earlier we have done in Page Cache we have to declare it's Duration attribute, when you request the Main Page label in Main page gets updated but the Label in User Control file is cached hence it will show the cached time (assuming that the duration of User Control file has not elapsed).
Step 1:
Create a Website name it Caching and rename the Default.aspx file to FragmentCache.aspx
Step 2:
Drag & Drop Label control on the Page with properties [ID= LblMsg ]
Step 3:
Write this code in FragmentCache.aspx.cs File.
protected void Page_Load(object sender, EventArgs e) { LblMsg.Text = "Current Time : "; LblMsg.Text += DateTime.Now.ToString(); }
The above code is for Main Page which will show system current Date & Time.
Step 4:
Now add a new Web User Control file in this project and name it UCPage.ascx
Step 5:
Again Drag & Drop Label control on the UCPage.ascx with properties [ID= LblMsg ]
Step 6:
Write this Line of code under Directive in PageCache.aspx File.
The above code will make this file cached for 10 secs.
Step 7:
Now Write This Line of Code in UCPage.ascx.cs File
protected void Page_Load(object sender, EventArgs e) { LblMsg.Text = "Cached Time : "; LblMsg.Text += DateTime.Now.ToString(); }
Step 8:
Now Drag & drop UCPage.acsx File from solution explorer on FragmentCache.aspx file by doing this, automatically these line of code are added in FragmentCache.aspx file.
When you debug FragmentCache.aspx page you will see that the page shows the current time in upper Label control while lower Label Control shows Cached time. Whether how many time you have refresh the page within 10 secs in will show same time, as soon as the time elapsed you will get the current system time in both Controls. This is how we implement Fragment cache in asp.net
Introduction:
This tutorial illustrates how to implement page caching, here I am creating a simple page that shows system current date & time in a Label control, here we have used Directive along with Directive to cache entire Page.
When we cache a Page we also have to declare it's Lifetime because if we don't declare it's lifetime than every time we request to the server & we will get the same old cached Page Hence it's mandatory to declare lifetime of a Cached Page so that after particular time Server refreshes that Page. Hence In OutputCache Directive we will use Duration attribute, when you again request the same page after that duration than a new cached page is send to you by the server.
Step 1:
Open a new Website name it Caching and rename the Default.aspx file to PageCache.aspx
Step 2:
Drag & Drop Label control on the Page with properties [ID= LblMsg ]
Step 3:
Write this Line of code under Directive in PageCache.aspx File.
I have already discuss the use of Duration attribute but here we have a new attribute VaryByParam by declaring this we can cache multiple version of a page based on parameters passed in it. The possible parameters we can pass are none, "*", any query string. For more details check with msdn.
Step 4:
and type this code in PageCache.aspx.cs File.protected void Page_Load(object sender, EventArgs e) { LblMsg.Text = "Current Time :
"; LblMsg.Text = DateTime.Now.ToString(); }
In this code we are saving current System time in Label [LblMsg] Control.
When you debug this page you will see that the page shows the same time for 30 sec whether how many time you have refresh the page as soon as the time elapsed you will get the current system time. This is how we use to cache a page you may be thinking that 30 sec is very short duration of time may be but for Web Servers where there are thousands of request are processed in a minute. Hence generally caching duration is decided by Website Traffic.