今天看到一个在aspx页中被<%@Page%>指令支持的、可以扩展一个类中属性的使用方式。在ASP.NET中你可以声明一个公共属性,然后在aspx页面的<%@Page%>指令属性中为其赋值。而在以前的版本中<%@Page%>指令仅支持一些特定的属性。
Cs类文件如下:
1
using System;
2
3
namespace DemoOfAvalon
4
{
5
public partial class _Default : System.Web.UI.Page
6
{
7
private string message = "blank";
8
public string Message
9
{
10
get
11
{
12
return message;
13
}
14
set
15
{
16
message = value;
17
}
18
}
19
protected void Page_Load(object sender, EventArgs e)
20
{
21
Response.Write("My Message:"+message);
22
}
23
}
24
}
25
using System;2

3
namespace DemoOfAvalon4
{5
public partial class _Default : System.Web.UI.Page6
{7
private string message = "blank";8
public string Message 9
{10
get11
{12
return message;13
}14
set15
{16
message = value;17
}18
}19
protected void Page_Load(object sender, EventArgs e)20
{21
Response.Write("My Message:"+message);22
}23
}24
}25

ASPX页面文件如下:
1
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DemoOfAvalon._Default" Message="My Test Message String" %>
2
3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5
<html xmlns="http://www.w3.org/1999/xhtml" >
6
<head runat="server">
7
<title>无标题页</title>
8
</head>
9
<body>
10
<form id="form1" runat="server">
11
<div>
12
13
</div>
14
</form>
15
</body>
16
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DemoOfAvalon._Default" Message="My Test Message String" %>2

3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">4

5
<html xmlns="http://www.w3.org/1999/xhtml" >6
<head runat="server">7
<title>无标题页</title>8
</head>9
<body>10
<form id="form1" runat="server">11
<div>12
13
</div>14
</form>15
</body>16
</html>当运行时,你将得到“My Test Message String”这条信息。是不是很酷呢?!

