我有一個母版頁和兩個網頁,WebForm1和WebForm2。在母版頁上有兩個LinkButtons以便轉到WebForm1或WebForm2。Page_Load在離開頁面時調用
當我點擊LinkButton轉到WebForm1時,WebForm1的Page_Load事件處理程序被調用並且Page.IsPostBack == false。到現在爲止還挺好。
然後,當我點擊進入WebForm2發生這種情況:
a) The Page_Load event handler for WebForm1 is called again and Page.IsPostBack == true.
b) Then the Page_Load event handler for WebForm2 is called and its Page_Load == false.
Vice versa when going back to WebForm1.
爲什麼當我要WebForm2是Page_Load中的WebForm1的叫什麼?我正在加載WebForm2而不是WebForm1。
對於所有頁面:AutoEventWireup =「true」。
<form id="form1" runat="server">
<div>
<p>This is MySite.Master.</p>
<p>
<asp:LinkButton ID="goto1" runat="server" OnClick="goto1_Click">Go To WebForm1</asp:LinkButton>
</p>
<p>
<asp:LinkButton ID="goto2" runat="server" OnClick="goto2_Click">Go To WebForm2</asp:LinkButton>
</p>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
protected void goto1_Click(object sender, EventArgs e) {
Response.Redirect("WebForm1.aspx");
}
protected void goto2_Click(object sender, EventArgs e) {
Response.Redirect("WebForm2.aspx");
}
public partial class WebForm1 : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
if (Page.IsPostBack) {
}
}
}
public partial class WebForm2 : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
if (Page.IsPostBack) {
}
}
}
這是asp.net方式按鈕的作用。他們導致頁面回發,這就是爲什麼page_load再次被擊中。那麼點擊處理程序會觸發。 –
@ S.Akbari請參閱下面的回答 –