ASP.NET Tutorial/Page Lifecycle/Page Directives

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

ASP.NET Page Directives

   <source lang="csharp">

Directive Description @Page Define page-specific attributes @Control Define control-specific attributes. @Import Import a namespace into a page. @Register Associate aliases with namespaces and class names @Assembly Link an assembly against the current page. @OutputCache Control the output caching policy for the page.</source>


Debugging ASP.NET Pages

   <source lang="csharp">

<%@ Page Language="C#" Debug="true" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">

   void Page_Load()
   {
       int zero = 0;
       Label1.Text = (1 / zero).ToString();
   }

</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <title>Show Error</title>

</head> <body>

   <form id="form1" runat="server">
   <asp:Label
       id="Label1"
       Runat="server" />
   </form>

</body> </html></source>


Displaying a localized Calendar control.

   <source lang="csharp">

<%@ Page Language="C#" Culture="id-ID" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <title>Show Calendar</title>

</head> <body>

   <form id="form1" runat="server">
   <asp:Calendar
       id="Calendar1"
       Runat="server" />
   </form>

</body> </html></source>


EnableViewState="True"

   <source lang="csharp">

<%@ Page Language="vb" EnableViewState="True" %> <html>

  <head>
     <title></title>
     <script runat="server">
        Sub Page_Load()
           If Page.EnableViewState = True Then
              Message.Text = "ViewState is enabled."
           Else
              Message.Text = "ViewState is disabled."
           End If
        End Sub
     </script>
  </head>

<body>

  <form runat="server">
     <asp:label id="Message" runat="server"/>
  </form>

</body> </html></source>


In page derivative, set trace to true

   <source lang="csharp">

<%@ Page Language="C#" Trace="true" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">

   void Page_Load()
   {
       Label1.Text = "nfex!";
       Calendar1.TodaysDate = DateTime.Now;
   }

</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <title>Show Trace</title>

</head> <body>

   <form id="form1" runat="server">
   <asp:Label
       id="Label1"
       Runat="server" />
   <asp:Calendar
       id="Calendar1"
       TodayDayStyle-BackColor="Yellow"
       Runat="server" />
   </form>

</body> </html></source>


Master page

   <source lang="csharp">

<%@ Page MasterPageFile="Default.master" %> <asp:Content id="myContent" runat="server"

   ContentPlaceHolderID="FrontPageContent">
   Welcome to our web site! We hope you"ll enjoy your visit.

</asp:Content>

File: Default.master <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

<html>

 <head>
   <title>Front Page</title>
 </head>
 <body>
   <form id="myForm" runat="server">

Welcome to SuperSite Inc!

     <asp:ContentPlaceHolder id="FrontPageContent"
         runat="server" />
Copyright 2006
   </form>
 </body>

</html></source>


Session State - Disabled

   <source lang="csharp">

<%@ Page EnableSessionState = "false" %> <script runat="server" >

 Public Sub Page_Load
    Session("MySession") = "My Session Value"
    Response.Write( Session("MySession") )
 End Sub

</script> <HTML>

   <form id="Form1" runat="server">
      When EnableSessionState = "false" Session State is not accessible - this page generates an exception.
   </form>
   </body>

</html></source>


Session State - Readonly

   <source lang="csharp">

<%@ Page EnableSessionState = "ReadOnly" %> <script runat="server" >

 Public Sub Page_Load
    Response.Write( Session("MySession") )
    Session("MySession") = "My Session Value"
 End Sub

</script> <HTML>

   <form id="Form1" runat="server">
      A session state value is set, but is not persisted when EnableSessionState = "ReadOnly".
   </form>
   </body>

</html></source>


Setting a Culture Manually

   <source lang="csharp">

<%@ Page Language="C#" Culture="id-ID" UICulture="id-ID" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server">

   void Page_Load()
   {
       lblDate.Text = DateTime.Now.ToString("D");
       lblPrice.Text = (512.33m).ToString("c");
   }

</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <title>Bagus</title>

</head> <body>

   <form id="form1" runat="server">
   Today"s date is:
   
<asp:Label id="lblDate" Runat="server" />

   The price of the product is:
   
<asp:Label id="lblPrice" Runat="server" />
   </form>

</body> </html></source>


Take advantage of page-level tracing

   <source lang="csharp">

<%@ Page Language="C#" Trace="true" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">

   void Page_Load()
   {
       for (int counter = 0; counter < 10; counter++)
       {
           ListBox1.Items.Add("item " + counter.ToString());
           Trace.Warn("counter=" + counter.ToString());
       }
   }

</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <title>Page Trace</title>

</head> <body>

   <form id="form1" runat="server">
   <asp:ListBox
       id="ListBox1"
       Runat="server" />
   </form>

</body> </html></source>