方法一(2.0): <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SimplePageWithCodeBehind.aspx.cs" Inherits="EssentialAspDotNet.SimplePageWithCodeBehind" Debug="true" %> public partial class SimplePageWithCodeBehind : Page { string[] _displayItemData = {"Item #1", "Item #2", "Item #3", "Item #4", "Item #5", "Item #6", "Item #7", "Item #8", "Item #9", "Item #10"}; protected override void OnLoad(EventArgs e) { _messageH2.InnerText = "Total number of items = " + _displayItemData.Length.ToString(); _displayList.DataSource = _displayItemData; _displayList.DataBind(); base.OnLoad(e); } } 方法二(1.0): <%@ Page Language="C#" AutoEventWireup="true" Src="SimplePageWithCodeBehindV1.aspx.cs" Inherits="EssentialAspDotNet.SimplePageWithCodeBehindV1" %> public class SimplePageWithCodeBehindV1 : Page { protected BulletedList _displayList; protected HtmlGenericControl _messageH2; string[] _displayItemData = {"Item #1", "Item #2", "Item #3", "Item #4", "Item #5", "Item #6", "Item #7", "Item #8", "Item #9", "Item #10"}; protected override void OnLoad(EventArgs e) { _messageH2.InnerText = "Total number of items = " + _displayItemData.Length.ToString(); _displayList.DataSource = _displayItemData; _displayList.DataBind(); base.OnLoad(e); } } 方法三(前端继承方法): <%@ Page Language="C#" Debug="true" Trace="true" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <mce:script runat="server"><!-- const int _itemCount = 10; string GetDisplayItem(int n) { return "Item #" + n.ToString(); } protected override void OnLoad(EventArgs e) { // Clear out items populated by static declaration _displayList.Items.Clear(); for (int i=0; i<_itemCount; i++) _displayList.Items.Add(new ListItem(GetDisplayItem(i))); _messageH2.InnerText = "Total number of items = " + _itemCount.ToString(); base.OnLoad(e); } // --></mce:script> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Simple page with controls</title> </head> <body> <form runat="server" id="_form" > <h1>Test ASP.NET 2.0 Page with controls</h1> <asp:BulletedList runat="server" ID="_displayList"> <asp:ListItem>Sample Item 1</asp:ListItem> <asp:ListItem>Sample Item 2</asp:ListItem> <asp:ListItem>Sample Item 3</asp:ListItem> </asp:BulletedList> <h2 runat="server" id="_messageH2">Total number of items = xx</h2> </form> </body> </html> 数据源绑定式: <mce:script runat="server"><!-- string[] _displayItemData = {"Item #1", "Item #2", "Item #3", "Item #4", "Item #5", "Item #6", "Item #7", "Item #8", "Item #9", "Item #10"}; protected override void OnLoad(EventArgs e) { _messageH2.InnerText = "Total number of items = " + _displayItemData.Length.ToString(); _displayItems.DataSource = _displayItemData; _displayItems.DataBind(); base.OnLoad(e); } // --></mce:script> 方法四(前端申明): <%@ 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"> <mce:script runat="server"><!-- const int _itemCount = 10; // Control declarations protected BulletedList _displayList; protected HtmlGenericControl _messageH2; protected HtmlForm _form; string GetDisplayItem(int n) { return "Item #" + n.ToString(); } protected override void OnLoad(EventArgs e) { // Clear out items populated by control initialization _displayList.Items.Clear(); for (int i = 0; i < _itemCount; i++) _displayList.Items.Add(new ListItem(GetDisplayItem(i))); _messageH2.InnerText = "Total number of items = " + _itemCount.ToString(); base.OnLoad(e); } // In the actual ASP.NET parsed page this work is done in the virtual method FrameworkInitialize // which we can't override here because the sibling partial class always overrides it. // protected override void OnPreInit(EventArgs e) { Controls.Add(new LiteralControl("/r/n/r/n<!DOCTYPE html PUBLIC /"-//W3C//DTD " + "XHTML 1.0 Transitional//EN/" /"http://www.w3" + ".org/TR/xhtml1/DTD/xhtml1-transitional.dtd/">/r/n/r/n")); Controls.Add(new LiteralControl("/r/n/r/n<html xmlns=/"http://www.w3.org/1999/xhtml/" >" + "/r/n<head>/r/n <title>Simple page " + "with controls</title>/r/n</head>/r/n<body>/r/n")); _form = new HtmlForm(); _form.Controls.Add(new LiteralControl("<h1>Test ASP.NET 2.0 Page with controls</h1>/r/n ")); _displayList = new BulletedList(); _displayList.ID = "_displayList"; _displayList.Items.Add(new ListItem("Sample Item 1")); _displayList.Items.Add(new ListItem("Sample Item 2")); _displayList.Items.Add(new ListItem("Sample Item 3")); _form.Controls.Add(_displayList); _messageH2 = new HtmlGenericControl("h2"); _messageH2.ID = "_messageH2"; _messageH2.Controls.Add(new LiteralControl("Total number of items = xx")); _form.Controls.Add(_messageH2); Controls.Add(_form); Controls.Add(new LiteralControl("/r/n/r/n</body>/r/n</html>/r/n")); base.OnPreInit(e); } // --></mce:script> 数据绑定式: <%@ 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"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Simple page with declarative data binding</title> </head> <body> <form runat="server" id="_form"> <h1>Test ASP.NET 2.0 Page with declarative data binding</h1> <asp:BulletedList runat="server" ID="_displayItems" DataSourceID="_itemsDataSource"> <asp:ListItem>Sample item 1</asp:ListItem> <asp:ListItem>Sample item 2 ...</asp:ListItem> </asp:BulletedList> <h2 runat="server" id="_messageH2">Total number of items = xx</h2> <asp:ObjectDataSource runat="server" ID="_itemsDataSource" TypeName="EssentialAspDotNet.Architecture.MyDataSource" SelectMethod="GetItems" /> </form> </body> </html> 自定义数据源: using System; namespace EssentialAspDotNet.Architecture { public static class MyDataSource { static string[] _items = {"Item #1", "Item #2", "Item #3", "Item #4", "Item #5", "Item #6", "Item #7", "Item #8", "Item #9", "Item #10"}; public static string[] GetItems() { return _items; } } }