ASP.NET - Directives - Page

The page directive in ASP.NET is used to provide instructions and configuration information for a specific web page. It is usually the first directive in an ASP.NET page and begins with the @Page keyword. Here are some examples of how the page directive can be used:

Setting the language of the page:

<%@ Page Language="C#" %>

This page directive specifies that the code in the page will be written in the C# programming language. If you are using Visual Basic .NET, you would replace "C#" with "VB".

Specifying the master page:

<%@ Page MasterPageFile="~/Site.Master" %>

This page directive specifies that the current page uses the Site.Master file as its master page. The tilde (~) character is used to represent the root of the web application.

Setting the title of the page:

<%@ Page Title="My ASP.NET Page" %>

This page directive specifies the title of the current page, which will be displayed in the title bar of the web browser.

Adding a code-behind file:

<%@ Page Language="C#" CodeFile="MyPage.aspx.cs" Inherits="MyPage" %>

This page directive specifies that the code for the page will be stored in the MyPage.aspx.cs file, and that the class name for the code-behind file is "MyPage".

Disabling ViewState:

<%@ Page EnableViewState="false" %>

This page directive disables the ViewState feature for the current page. ViewState is used to maintain the state of controls on the page between postbacks, but it can also increase the size of the page and affect performance.

Setting the theme of the page:

<%@ Page Theme="MyTheme" %>

This page directive specifies the name of the theme that will be used for the current page. Themes are used to apply a consistent look and feel to an entire web application.

These are just a few examples of how the page directive can be used in ASP.NET. By using different attributes in the page directive, you can configure various aspects of your web page, such as its language, title, master page, code-behind file, ViewState, and theme.