๐งพ ASP.NET โ Directives โ Configure Page Behavior at Compile-Time
๐งฒ Introduction โ What Are ASP.NET Directives?
ASP.NET directives are special instructions used in .aspx
and other ASP.NET files to control how pages and controls are compiled, processed, and rendered. They appear at the top of a page and are enclosed within <%@ ... %>
tags.
Directives define important settings such as:
- Programming language
- Code-behind files
- Master pages
- Namespaces to import
- Output caching behavior
๐ฏ In this guide, youโll learn:
- Different types of ASP.NET directives
- How to use the
@Page
,@Control
,@Master
,@Import
,@OutputCache
, and others - Real examples with explanation of how each affects page behavior
๐งฑ Common ASP.NET Directives Overview
๐งพ Directive | ๐ Description |
---|---|
@Page | Defines settings for .aspx pages (language, code file, etc.) |
@Control | Used in user controls (.ascx ) |
@Master | Used in master pages (.master ) |
@Import | Imports namespaces |
@Register | Links custom controls and user controls with a tag prefix |
@OutputCache | Enables output caching for the page |
@Reference | References other pages or user controls |
@Assembly | Links to external assemblies |
โ
@Page
Directive โ For .aspx
Pages
Placed at the top of every .aspx
page to configure its compilation and execution.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MyApp.Default" %>
๐ Explanation:
Language="C#"
: Sets the programming languageAutoEventWireup="true"
: Automatically binds events likePage_Load
CodeBehind
: Points to the backend fileInherits
: Specifies the class that inherits fromSystem.Web.UI.Page
โ
@Control
Directive โ For User Controls
Used in .ascx
files to define control settings.
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyControl.ascx.cs" Inherits="MyApp.MyControl" %>
โ
@Master
Directive โ For Master Pages
Specifies the class and language for master pages (.master
).
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="MyApp.SiteMaster" %>
โ
@Import
Directive โ Import Namespaces
Allows the page to access external .NET namespaces.
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Text" %>
๐ Used to access ADO.NET, string builders, collections, etc.
โ
@Register
Directive โ Use Custom or User Controls
<%@ Register TagPrefix="uc" TagName="Header" Src="~/Controls/Header.ascx" %>
๐ Usage in page body:
<uc:Header runat="server" ID="ucHeader" />
โ
@OutputCache
Directive โ Enable Page Caching
<%@ OutputCache Duration="60" VaryByParam="None" %>
๐ Explanation:
Duration="60"
: Cache output for 60 secondsVaryByParam
: Can vary output based on query string or form input
โ
@Reference
Directive โ Reference Other Pages/Controls
Used for strongly-typed access to previous pages.
<%@ Reference Page="~/PreviousPage.aspx" %>
๐ Useful for cross-page posting and accessing properties of the previous page.
โ
@Assembly
Directive โ Load External DLLs
<%@ Assembly Name="MyLibrary" %>
๐ Allows use of external assemblies without adding reference in project file.
๐งช Example โ Page with Multiple Directives
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SampleApp._Default" %>
<%@ Import Namespace="System.Data" %>
<%@ Register TagPrefix="uc" TagName="Footer" Src="~/Controls/Footer.ascx" %>
<%@ OutputCache Duration="30" VaryByParam="None" %>
๐ Best Practices for ASP.NET Directives
โ Do:
- Always use
@Page
with properCodeBehind
andInherits
- Use
@OutputCache
for performance boost - Keep directives at the very top of the file
โ Avoid:
- Mixing directive types (e.g.,
@Page
in.ascx
) - Forgetting
@Register
when using user controls - Overusing
@Import
โimport only necessary namespaces
๐ Summary โ Recap & Next Steps
ASP.NET directives are essential for configuring how pages and controls behave, compile, and cache. They are the building blocks for how the server interprets and processes your ASP.NET files.
๐ Key Takeaways:
@Page
is mandatory for.aspx
pages- Use
@Register
to integrate user/custom controls @OutputCache
can improve page load speed
โ๏ธ Real-world Use Cases:
- Control partial caching on frequently hit pages
- Load third-party libraries via
@Assembly
- Build modular websites with registered controls
โ FAQs โ ASP.NET Directives
โ Where should I place directives in ASP.NET pages?
โ
Always at the very top of the page, before any HTML or server-side script.
โ Can I use multiple @Import
directives in one page?
โ
Yes. You can use as many as needed for different namespaces.
โ What is the purpose of AutoEventWireup
?
โ
If true
, ASP.NET automatically connects event methods (like Page_Load
) based on naming convention.
โ Is @Register
required for every user control?
โ
Yes. Every custom or user control must be registered with a unique tag prefix and tag name.
Share Now :