Page_Load is called when the page exits.

I have a main page and two web pages, WebForm1 and WebForm2. There are two LinkButtons on the main page to go to WebForm1 or WebForm2.

When I click LinkButton to go to WebForm1, the Page_Load event handler for WebForm1 and Page.IsPostBack == false are called. So far so good.

Then, when I click to go to WebForm2, this happens:

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.

Why is Page_Load called for WebForm1 when I go to WebForm2? I am loading WebForm2, not WebForm1.

For all pages: 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) {

        }
    }
}
+4
source share
2 answers

To add Kirk to the answer ...

, LinkButton . LinkButton - , , - javascript, ASP.NET .

, , HTML:

<a href="WebForm2.aspx">Go To WebForm2</a>
+2

, , goto1 goto2 , , . , .

ASP.NET

MasterPage Content

, ,

  • PreInit.
  • - Init.
  • Init.
  • Init.
  • .
  • .
  • .
  • - .
  • .
  • PreRender.
  • PreRender.
  • - PreRender.
  • PreRender.
  • - .
  • .
  • .
  • .

PostBack,

Page_Load Click, PostBack, WebForm1

Response.Redirect("WebForm2.aspx");

.

+4

Source: https://habr.com/ru/post/1648045/


All Articles