Page Directive in ASP.Net

The page directive is the first component inside an ASP web form. It is used to provide various instructions to the server, It provides the following information:

  1. What language we are using for the web page.
  2. Whether there themes or skins attached to the page.
  3. Trace is enabled or not.
  4. Linking to the code file of the page using the CodeFile attribute.
  5. Disabling session and other state management elements in the page label and so on.
  6. Enable debugging in the page.

For a number of requirements we are using the page directive.

  • In simple words the page directive is nothing but an instruction to the server that the page will be generated with the following properties.
  • The syntax for the page directive comes with:
    1. <%@ page directive options%>  
  • The developer must define all the properties of the page in the page directive so that when it renders it should follow these properties.

When we are adding a page to our project the page will contain the following default properties.

  1. Language
  2. AutoEventWireup
  3. CodeFile
  4. Inherits

We can set the page directive as follows:

set the page directive

Let's explain each attribute one by one with its role.

Language

The language property specifies the language you are doing the project in. Your Language could be "C#" or "VB".

Language

For example, Language="C#".

AutoEventWireup

AutoEventWireup is a Boolean property indicating whether or not the ASP.NET page label event is automatically wired with the respective events.

The Page Label events is the event of a page such as the following:

    page Load,Preinit,init,and so on.

This property is by default is set to true.

    eg-AutoEventWireup="true"

CodeFile

A webpage contains mainly the following two parts.

  1. Design part
  2. Logic or coding part

web page

  • A Web page contains 2 parts, the Design Part and the Logic Part.
  • In the design part we need to design the page.
  • In the logic part we need to write the logic, database connection and so on.
So in order to link this design part to the code behind part we use the “CodeFile” property in the web page.

Inherits

The name of the class from which it inherits.

In ASP.NET the code-behind (or code-beside for version 2.0) models an .aspx page and the code file for it is connected in two ways. The CodeBehind (or CodeFile for 2.0) attribute in the Page directive contains the physical filename (like Mypage.aspx.cs) whereas the Inherits contains the class (or partial class for 2.0) for the page. Renaming the page changes the filenames and hence the CodeBehind/CodeFile attributes value. However it does not need to and therefore also doesn't rename the class in the codefile. Inherits should only be changed if the name of the class is also changed manually.

Up Next
    Ebook Download
    View all
    Learn
    View all