ASP.NET Tutorial/Page Lifecycle/Page Directives

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

ASP.NET Page Directives

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.


Debugging ASP.NET Pages

<%@ 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">
    <div>
    <asp:Label
        id="Label1"
        Runat="server" />
    </div>
    </form>
</body>
</html>


Displaying a localized Calendar control.

<%@ 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">
    <div>
    <asp:Calendar
        id="Calendar1"
        Runat="server" />
    </div>
    </form>
</body>
</html>


EnableViewState="True"

<%@ 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>


In page derivative, set trace to true

<%@ 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">
    <div>
    <asp:Label
        id="Label1"
        Runat="server" />
    <asp:Calendar
        id="Calendar1"
        TodayDayStyle-BackColor="Yellow"
        Runat="server" />
    </div>
    </form>
</body>
</html>


Master page

<%@ 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">
      <h1>Welcome to SuperSite Inc!</h1>
      <asp:ContentPlaceHolder id="FrontPageContent"
          runat="server" />
      <pre>Copyright 2006</pre>
    </form>
  </body>
</html>


Session State - Disabled

<%@ 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>


Session State - Readonly

<%@ 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>


Setting a Culture Manually

<%@ 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">
    <div>
    Today"s date is:
    <br />
    <asp:Label
        id="lblDate"
        Runat="server" />
    <hr />
    The price of the product is:
    <br />
    <asp:Label
        id="lblPrice"
        Runat="server" />
    </div>
    </form>
</body>
</html>


Take advantage of page-level tracing

<%@ 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">
    <div>
    <asp:ListBox
        id="ListBox1"
        Runat="server" />
    </div>
    </form>
</body>
</html>