序
HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!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 runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:DropDownList ID="ddlTest" runat="server" /><asp:Button ID="btnTest" runat="server" Text="Test"/> </div> </form> </body> </html>
CS
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Init(object sender, EventArgs e) { ddlTest.Items.Add(new ListItem("Init", "Init")); } protected void Page_Load(object sender, EventArgs e) { ddlTest.Items.Add(new ListItem("Load", "Load")); } }
最近看見有朋友說Page_Init只執行一次,我始終將信將疑,於是自己測試一下,結果發現在每次單擊按鈕的時候還真的不會再增加Init這樣的列表項了。難道Page_Init還真的是只執行一次麼?空想也不是解決辦法,打個斷點跟蹤下吧,發現每次回發時Init是會執行的,由此可見出現這種情況是和Init無關的(她是無辜的)。真正的幕後'黑手'又會是誰呢?