重新导航时未调用 Page_Load

发布于 2024-07-27 00:20:22 字数 1182 浏览 9 评论 0原文

我有一个奇怪的问题。 这是场景

这是我的文件:

  1. Project1.aspx
  2. Project2.aspx

它们的设置完全相同,包括它们的 Page_Load 函数:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

If (Not Page.IsPostBack) Then

   setPrevIndex(-1)

   ...

   End If


End Sub

它们都是这样设置的。

这是我遇到问题的地方。 当我导航到这些页面中的任何一个时,我需要确保 prevIndex 设置为 -1(通过该函数)。

  • 对于 Project1.aspx 当我导航时 到页面时,Page_Load 被触发。
  • 对于Project1.aspx,当我刷新页面时,Page_Load 会触发。
  • 对于 Project1.aspx,当我在浏览器导航栏中按“Go”键返回当前页面时,将触发 Page_Load。

  • 对于 Project2.aspx 当我导航时 到页面时,Page_Load 被触发。
  • 对于 Project2.aspx 当我刷新时 页面,Page_Load 被触发。
  • 对于 Project2.aspx 当我按下 我的浏览器导航栏中的“前往”, 返回当前页面, Page_Load 根本不触发! 该功能甚至没有 叫。

有什么想法为什么??? 什么会导致这种情况呢?

请要求澄清。

更新:

当我在指向同一 URL 的 URL 中按“Go”时,母版页似乎是唯一重新加载的内容,但 Load_Page 事件甚至没有触发...

还有其他建议吗?

谢谢, 乙

I am having a strange problem. Here is the scenario

Here are my files:

  1. Project1.aspx
  2. Project2.aspx

They are set up the exact same, including their Page_Load functions:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

If (Not Page.IsPostBack) Then

   setPrevIndex(-1)

   ...

   End If


End Sub

They are both set up this way.

Here is where I am running into a problem. When I navigate to either of these pages, i need to make sure that prevIndex is set to -1 (via the function).

  • For Project1.aspx when I navigate
    to the page, the Page_Load fires.
  • For Project1.aspx when I refresh the page, the Page_Load fires.
  • For Project1.aspx when I press "Go" in my browsers navigation bar, traveling back to the current page, the Page_Load fires.

  • For Project2.aspx when I navigate
    to the page, the Page_Load fires.
  • For Project2.aspx when I refresh
    the page, the Page_Load fires.
  • For Project2.aspx when I press
    "Go" in my browsers navigation bar,
    traveling back to the current page,
    the Page_Load doesn't fire at all! The function isn't even
    called.

Any ideas why??? What would cause this?

Please ask for clarification.

Update:

When I press "Go" in the URL pointing to the same URL, it seems like the masterpage is the only thing that re-loads, but the Load_Page event doesn't even fire...

Any other suggestions?

Thanks,
E

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

眼泪淡了忧伤 2024-08-03 00:20:22

尝试禁用输出缓存并查看问题是否仍然出现:

<system.web>
  <caching>
    <outputCache enableOutputCache="false"/>
  </caching>
<system.web>

Try disabling output cache and see if the problem still occurs:

<system.web>
  <caching>
    <outputCache enableOutputCache="false"/>
  </caching>
<system.web>
薄暮涼年 2024-08-03 00:20:22

使用 LiveHTTPHeadersFiddler 确保每次实际上都以相同的方式请求页面。 这可能是缓存的问题。

Use LiveHTTPHeaders or Fiddler to make sure the page is actually being requested the same way each time. This may be an issue with caching.

无人接听 2024-08-03 00:20:22

在本地加载您的网站并转到 http://yourwebsite/trace.axd

这会显示每个页面的服务器跟踪以及服务器状态。 它还显示了完整的页面生命周期和时间。

清除当前跟踪,然后重复 3 次访问,并将每次访问重新加载到 Project1.aspx 和 Project2.aspx

Trace.axd 现在显示什么? 您应该有 6 个条目,每个条目都有状态代码 200 和动词 GET。

如果你的数量较少,那么你的问题就是某种缓存。

如果有 6 个,请检查最后一个的详细信息 - 页面事件生命周期显示什么? 它还将显示完整的 WebForm 控件层次结构,因此如果这与母版页相关,您将能够分辨出来。

Load up your website locally and go to http://yourwebsite/trace.axd

This shows a server trace for each page, along with the server status. It also shows the complete page lifecycle with timings.

Clear the current trace and then repeat your 3 visits and reloads each to Project1.aspx and Project2.aspx

What does trace.axd show now? You should have 6 entries, each with the status code of 200 and the verb GET.

If you have fewer then your problem is caching of some sort.

If you have 6 then check the details for the last one - what does is show for the page event lifecycle? It will also show the complete WebForm control hierarchy, so if this related to the master page you will be able to tell.

庆幸我还是我 2024-08-03 00:20:22

尝试在页面初始化事件中将 previndex 设置为 -1。 我不确定为什么会发生这种情况。

Try Setting the previndex to -1 in Page Init Event. I am not sure why this is happening though.

情魔剑神 2024-08-03 00:20:22

各种与缓存相关的事情都可能导致请求无法发出,特别是当您仅按“执行”按钮时,因此您应该检查缓存标头。

如果缓存是问题所在,您可以执行以下操作:

//ask browser to revalidate:
context.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches); 
//and hint that the page is outdated anyhow...
context.Response.Cache.SetMaxAge(TimeSpan.Zero);

这应该说服浏览器在每次浏览量时真正获得新版本。 例如,您可以在 Page_Load 本身中设置这些变量;-)。 如果您不使用 https,那么以下内容也是无风险的:

//prevents plugin based file-open in IE+https, otherwise fine:
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.Cache.SetNoStore();

我敢打赌这可以解决问题 - 如果没有,我同意 Jeremy Steins 建议您使用 fiddler 来验证请求是否确实发出(并且由于您是网络开发人员,无论如何都要获取 fiddler,这是一个方便的工具可用,并且适用于所有浏览器!)。

最后 - 您能否判断当您单击“开始”时页面上是否有任何其他代码运行? (即整个页面没有运行,或者只是 Page_Load - 后者会提示事件连接错误,考虑到您的加载处理程序有时会触发,这会很奇怪)。

Various cache-related things can cause the request not to be made, in particular when you merely press the "go" button, so you should check your cache-headers.

If caching is the issue, you can do something like:

//ask browser to revalidate:
context.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches); 
//and hint that the page is outdated anyhow...
context.Response.Cache.SetMaxAge(TimeSpan.Zero);

Which should convince a browser to really get a new version every pageview. You could, for instance, set these variables in the Page_Load itself ;-). If you're not using https, then the following are risk-free too:

//prevents plugin based file-open in IE+https, otherwise fine:
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.Cache.SetNoStore();

I'll bet this solves it - and if not, I second Jeremy Steins suggestion that you use fiddler to verify that the request is really being made at all (and since you're a web-dev, get fiddler in any case, it's a handy tool to have available, and works for all browsers!).

Finally - can you tell whether any other code on the page runs when you click go? (i.e. is the entire page not running, or just Page_Load - the latter would suggest an event wire up error, which would be odd considering your load handler does sometimes fire).

疾风者 2024-08-03 00:20:22

尝试在服务器上使用精简的功能重新创建该场景。 如果问题仍然存在,请尝试使用一些缓存计数器方法。 如果不是,则意味着它必须是您的代码。

Try to recreate the scenario with stripped down functions on your server. If the problem persists, try to use some cache-countering methods. If not, that means it has to be your code.

ゝ偶尔ゞ 2024-08-03 00:20:22

听起来您的页面已被缓存。 这将导致 Page_Load 不触发。 检查您是否没有在任何地方设置它。

It sounds like your page is cached. This would cause the Page_Load not to fire. Check that you have not set that anywhere.

待"谢繁草 2024-08-03 00:20:22

您是否尝试过将应用程序发布到另一台计算机? 可能是 IIS 在执行某些操作,因此请首先尝试消除它。 假设您的代码在两个方面都是相同的,只是页面名称不同(对 aspx 和 .cs 文件进行比较以进行验证),然后将您的应用程序移动到不同的服务器并重新测试。

如果它仍然发生,则确实一定是您的浏览器在执行缓存方面的操作。

Have you tried publishing the application to a different machine? It could be IIS doing something so try and eliminate that first. Assuming that your code is identical in both na only the page names differ (do a diff on the aspx and .cs files to verify) then move your application to a different server and re-test.

If it is still occurring the it really must be your browser doing something probably with respect to caching.

故乡的云 2024-08-03 00:20:22

如果您的页面(或母版页)顶部有此(或类似内容),这将导致:

<%@ OutputCache Duration="3600" VaryByParam="none" %>

If you have this (or similar) at the top of your page (or master page) this will cause it:

<%@ OutputCache Duration="3600" VaryByParam="none" %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文