ASP.NET Tutorial/Page Lifecycle/Page Load

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

The script block holds a subroutine named Page_Load, which is triggered when the page is first created. (C#)

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

public delegate void PriceChangedEventHandler();
public class Product
{
    private string name;
    private decimal price;
    private string imageUrl;
    public string Name
    {
        get
        { return name; }
        set
        { name = value; }
    }
    public event PriceChangedEventHandler PriceChanged;
    public decimal Price
    {
        get
        { return price; }
        set
        {
            price = value;
            if (PriceChanged != null)
            {
                PriceChanged();
            }
        }
    }
    public string ImageUrl
    {
        get
        { return imageUrl; }
        set
        { imageUrl = value; }
    }
    public string GetHtml()
    {
        string htmlString;
        htmlString = "<h1>" + name + "</h1><br>";
        htmlString += "<h3>Costs: " + price.ToString() + "</h3><br>";
        htmlString += "<img src="" + imageUrl + "" />";
        return htmlString;
    }
    public Product(string name, decimal price)
    {
        Name = name;
        Price = price;
    }
}

    private void Page_Load(object sender, EventArgs e)
    {
        Product saleProduct = new Product("A", 49.99M);
        saleProduct.ImageUrl = "a.jpg";
        Response.Write(saleProduct.GetHtml());
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Product Test</title>
</head>
<body></body>
</html>