主页上没有 Page_PreLoad?
我在会话中有一个自定义对象,它通过母版页和主页上的回发进行更新。
回发后,我需要获取 Session 对象,重建和修改它的部分或整个对象,然后将其加载回 Session。
我已经在主页的Page_Load中编写了这段代码。它仅在一页上运行良好。
现在我已经编写了具有相同母版页的另一个页面,并且我希望母版页能够像以前一样修改我的会话对象。
所以我想我只需要将会话处理代码移至母版页的 Page_Load 即可。但这并没有像我预期的那样工作,因为主页上的控件(即中继器)在母版页的 Page_Load 触发之前访问 OnItemDataBound 事件处理程序中的会话对象,这样它只能获取会话对象的先前状态。 (只有主页上的转发器是这样,母版页上的转发器在访问会话时获取当前状态)
无论我认为我可以使用母版页的Page_PreLoad事件,我都可以访问中的回发数据Page_PreLoad 也很好,并相应地更新会话对象,但我发现母版页上没有 Page_PreLoad,或者我无法使用它。
我应该在会话中的哪里更新我的对象?
总结一下:我需要在母版页的代码隐藏中放置一个位置,以便可以使用回发数据,并且主页和母版页的中继器的 OnItemDataBound 事件都尚未被触发。
I have a custom object in the Session which updates by postbacks on both the masterpage and the main page.
After the postback, I need to get the Session object, rebuild and modify parts of it or the whole object, and load it back into the Session.
I have written this code in the Page_Load of the main page. It works fine on only one page.
Now I have written an other page with the same masterpage, and I want the masterpage to be able to modify my session object just like before.
So I thought I just need to move the session handling code to the masterpage's Page_Load. But that did not work as I expected, as a control on the main page (namely a repeater) accesses the session object in the OnItemDataBound event's handler, BEFORE the masterpage's Page_Load fires, and this way it only gets the previous state of the session object. (It is only true for the repeater on the main page, the repeater on the masterpage gets the current state when it accesses the session)
No matter I thought I could use the Page_PreLoad event of the masterpage, I could access the postback data in the Page_PreLoad just as fine, and update the session object accordingly, but I found out there is NO Page_PreLoad on the masterpage, or I fail to use it.
Where should I update my object in the session?
To sum it up: I need a place in the masterpage's codebehind where postback data is ready to use, and neither of the main page's nor the masterpage's repeater's OnItemDataBound event have been fired yet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
还有另一种解决方案:
在母版页的 init 事件中,您实际上可以订阅页面的预加载事件。
在母版页中考虑以下代码:
There is also another sollution:
in the init event of the masterpage you can actually subscribe for the preload event of the page.
consider this code in the masterpage:
希望我正确理解这一点 - 我认为最好的选择是创建一个基本页面,并使您的页面继承此页面。将您的逻辑放入基页的
Page_Load
或Page_PreLoad
事件处理程序中。母版页在实际页面开始加载后加载。因此,您创建一个基本页面:
并使您的页面继承此页面(以及使用您的母版页):
Hopefully I'm understanding this correctly - I think your best option is to create a base page, and make your pages inherit from this. Put your logic in the
Page_Load
orPage_PreLoad
event handler in the base page. Master pages are loaded after the actual page starts to load.So, you create a base page:
And make your page inherit from this (as well as using your master page):
来自 asp.net 论坛:
我最终只是使用了 Init 事件(因为没有 PreLoad 事件):
From asp.net forums:
i ended up simply using the
Init
event (since there is noPreLoad
event):如果您正在寻找此功能,您还可以覆盖预加载:
you can also override preload if you are looking for this: