c# – 离开页面时调用Page_Load
作者:互联网
我有一个母版页和两个网页,WebForm1和WebForm2.在母版页上有两个LinkButtons,以便转到WebForm1或WebForm2.
当我单击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 for 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) {
}
}
}
解决方法:
加上柯克的回答……
如果您只想要一个指向另一个页面的简单链接,请不要使用LinkButton. LinkButton只是一个提交按钮,它看起来像一个链接 – 它通过ASP.NET自动构建的javascript神奇地连接起来.
如果您想要链接只是将您发送到另一个页面,只需在常规HTML中执行:
<a href="WebForm2.aspx">Go To WebForm2</a>
标签:c,net,asp-net,master-pages 来源: https://codeday.me/bug/20190527/1166254.html