C# ASP.Net:Page_Load() 被多次调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1094954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:05:32  来源:igfitidea点击:

ASP.Net: Page_Load() being called multiple times

c#asp.net

提问by evilfred

I don't know alot about ASP.Net but I'm trying to make a new control for a message box. You enter some info and press a button.

我不太了解 ASP.Net,但我正在尝试为消息框制作一个新控件。您输入一些信息并按下按钮。

However, for some bizarre reason when the button is pressed, Page_Load() gets called a second time, and all of the member variables are reset to null! I need those variables, and Page_Load() has not reason to be called a second time! Of course the callstack is useless.

然而,由于一些奇怪的原因,当按钮被按下时,Page_Load() 被第二次调用,并且所有的成员变量都被重置为 null!我需要这些变量,而 Page_Load() 没有理由被第二次调用!当然,调用堆栈是无用的。

采纳答案by Joel Coehoorn

Remember, in ASP.Net every time you cause a postback of any kind, including handling events like button clicks, you're working with a brand new instance of your page class that must be rebuilt from scratch. Any work you've done previously to build the page on the server is gone. That means running the entirepage life cycle, including your page load code, and not just the click code.

请记住,在 ASP.Net 中,每次导致任何类型的回发(包括处理按钮单击等事件)时,您都在使用必须从头开始重建的页面类的全新实例。您之前为在服务器上构建页面所做的任何工作都没有了。这意味着运行整个页面生命周期,包括页面加载代码,而不仅仅是点击代码。

Always two there are, no more, no less. A request and a response.

总是有两个,不多也不少。一个请求和一个响应。

回答by Eugene

When the page posts back, the Page_Load method is called. Then, once the server actually processes the page and sends you a new one based on changes, the Page_Load is called again, actually the first time on the new page sent to you.

当页面回发时,调用 Page_Load 方法。然后,一旦服务器实际处理页面并根据更改向您发送一个新页面,就会再次调用 Page_Load,实际上是第一次在发送给您的新页面上。

So if you are pulling data in the Page_Load event or setting some values, enclose it in the following block:

因此,如果您在 Page_Load 事件中提取数据或设置一些值,请将其包含在以下块中:

if(!Page.IsPostBack)
{

}

to preserve some of your state. Otherwise, the instructions that you put into the Page_Load event will execute every time.

保留一些你的状态。否则,您放入 Page_Load 事件的指令将每次都执行。

It helps to review the ASP.Net page lifecycle :)

它有助于 ASP.Net 页面生命周期 :)

回答by Tim Meers

Just a shot in the dark but maybe add this after your page_load:

只是在黑暗中拍摄,但可以在您的 page_load 之后添加:

if (!IsPostBack)
{

回答by tbreffni

As Joel mentioned, instance variables will be lost once the page is sent back to the client. However, there are various methods of storing the values of your variables so you can retrieve them later. This page on State Managementis a good starting point if you want to learn more.

正如 Joel 所提到的,一旦页面被发送回客户端,实例变量就会丢失。但是,有多种方法可以存储变量的值,以便您以后可以检索它们。如果您想了解更多信息,这个关于状态管理的页面是一个很好的起点。

回答by anand

Any tag/element which requires url reference like img, anchor, object etc must be checked for the empty reference.

任何需要 url 引用(如 img、anchor、object 等)的标签/元素都必须检查空引用。

e.g. href="", url="", src="" are some common errors.

例如 href="", url="", src="" 是一些常见的错误。

回答by user3207728

This code works for me:

这段代码对我有用:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if (Session["something"] == null)
        {
            Session["something"] = "1";
        }
        else
        {
            Session["something"] = null;

            //your page load code here 

        }               
    }
}

回答by David d C e Freitas

For me, the issue was a bit complicated, but I found that the

对我来说,这个问题有点复杂,但我发现

protected override void OnPreRender(EventArgs e)

protected override void OnPreRender(EventArgs e)

handler is only called once, so it's safer to put some actions in there if it's not too late in the pipeline for you.

handler 只被调用一次,所以如果在管道中对你来说还不算太晚,把一些操作放在那里会更安全。

回答by Fandango68

An extension of @user3207728's response, I found this link explains it well and has a simple solution...

@user3207728 响应的扩展,我发现这个链接很好地解释了它并且有一个简单的解决方案......

http://www.aspdotnet-suresh.com/2010/04/detect-browser-refresh-to-avoid-events.html

http://www.aspdotnet-suresh.com/2010/04/detect-browser-refresh-to-avoid-events.html

Unfortunately checking just for if (!Page.IsPostBack)is not enough as IsPostBackwill always be FALSE on a refresh.

不幸的是,仅检查if (!Page.IsPostBack)是不够的,因为IsPostBack刷新时总是 FALSE。

回答by Rafi Ansari

you can use sessions or viewstate to retain the values of variables..

您可以使用会话或视图状态来保留变量的值..

  1. if you want to redirect to a different page , use session[]
  2. else if you want to stay on the same page , use viewstate[]
  1. 如果您想重定向到不同的页面,请使用 session[]
  2. 否则,如果您想停留在同一页面上,请使用 viewstate[]