Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a Page Directive in .NET

I'm studying up for a Microsoft Certification exam, and some of the wording for the 'content' in the examn confused me. In the MS exam website, under Developing Web Form Pages, it says in regard to the content on the exam...

This objective may include but is not limited to: page directives such as ViewState, request validation, event validation, MasterPageFile; ClientIDMode;

I was under the impression that a page directive referred to the @Page keyword, and the associated values defined in the @Page section were attributes/properties. But the wording of the exam content almost implies that the attributes/properties of the @Page keyword are directives.

Could someone please clear this up for me?

like image 433
contactmatt Avatar asked Oct 13 '22 22:10

contactmatt


1 Answers

I think their verbiage is a little confusing. You are correct that the values within the directive are essentially properties. In fact, you can set most of these AS properties within the object model (e.g. in the codebehind).

 protected override void OnInit( EventArgs e )
 {
        this.EnableViewState = true;
        this.MasterPageFile = "~/something.master";
        this.Title = "Hello World";
 }

Their are several important directives besides Page, such as Import and Control. I would suggest being familiar with these as well.

EDIT: I was curious where these attributes end up when set from the Page directive, so I took a look at the ASP.Net cache. As you probably know, when a page is executed it is turned into an object and cached on the file system in:

c:\windows\Microsoft.Net\Framework[version]\Temporary ASP.Net Files\

        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        private void @__BuildControlTree(content_shared_toolbarcontrol_ascx @__ctrl) {
            System.Web.UI.IParserAccessor @__parser = ((System.Web.UI.IParserAccessor)(@__ctrl));

            #line 1 "C:\dev\Web.UI\Content\Shared\ToolbarControl.ascx"
            @__parser.AddParsedSubObject(new System.Web.UI.LiteralControl("\r\n<div class=\"toolbar\">\r\n    "));

            #line default
            #line hidden
            global::System.Web.UI.HtmlControls.HtmlGenericControl @__ctrl1;

            #line 1 "C:\dev\Web.UI\Content\Shared\ToolbarControl.ascx"
            @__ctrl1 = this.@__BuildControldivDelete();

            #line default
            #line hidden

            #line 1 "C:\dev\Web.UI\Content\Shared\ToolbarControl.ascx"
            @__parser.AddParsedSubObject(@__ctrl1);

            #line default
            #line hidden

            #line 1 "C:\dev\Web.UI\Content\Shared\ToolbarControl.ascx"
            @__parser.AddParsedSubObject(new System.Web.UI.LiteralControl("\r\n    <div class=\"toolbarSpacer\"></div>\r\n    <div class=\"toolbarButton\">"));

et cetera

What I found interesting is that properties such as one might set in the Page directive are not included in these auto-generated files. This leads me to believe that the directive in its completeness is processed on each request and is not compiled along with the page.

EDIT #2: Per BurningIce's comment below, I dug into this further. I believe directive attributes can be described as follows.

Each of the attributes on a directive serves one or more of the following purposes: A hint to the compiler (such as the codebehind path), a hint to load the page (such as what class the page inherits from), a hint to render the page (such as MasterPageFile), and/or a property to set on each instance of the page that is created (such as Title).

like image 171
Tim M. Avatar answered Oct 18 '22 01:10

Tim M.