我们只能在 Page_PreRender 事件中编写什么样的代码?

发布于 2024-09-19 03:32:31 字数 555 浏览 5 评论 0原文

我在谷歌上搜索了 Page_preRender,我们如何使用 Page_PreRender 而不是 page_Load,或者我们不能使用 page_Load 而必须使用 Page_PreRender 的情况可能是什么,但我发现的每个地方,

对象预渲染的点是对对象的最后一次更改可以保存或保留到视图状态,这使得 PreRender 步骤成为进行最终修改的好地方,例如更改控件的属性或更改控件树结构,而不必担心 ASP.NET 进行更改。基于数据库调用或视图状态更新的对象。在 PreRender 阶段之后,这些对象的更改将被锁定,并且不能再保存到页面视图状态中。可以使用 OnPreRender 覆盖 PreRender 步骤。

但我无法理解哪些更改是我们无法在 page_Load 中执行而必须在 PreRender 中执行的。

据我所知,在 Page_PreInit 上我们可以动态创建控件,更改或分配 MasterPage 或主题,这是我们在 page_Load 或 Init 之后无法实现的。

但是 Page_PreRender 呢?哪些事情是我们只能在 Page_PreRender 中完成的。

I google about Page_preRender that how can we use Page_PreRender rather than page_Load or what could be the scenario where we could not use page_Load and have to use Page_PreRender but every place I find that,

"The point at which the objects are prerendered is the last time changes to the objects can be saved or persisted to viewstate. This makes the PreRender step a good place to make final modifications, such as changing properties of controls or changing Control Tree structure, without having to worry about ASP.NET making changes to objects based off of database calls or viewstate updates. After the PreRender phase those changes to objects are locked in and can no longer be saved to the page viewstate. The PreRender step can be overridden using OnPreRender".

but I could not understand that which sort of changes are which we could not do in page_Load and have to do in PreRender.

As I know that on Page_PreInit we can create Controls dynamically, change or assign MasterPage or Theme which we could not in page_Load or after Init.

But what about Page_PreRender what are things which we can only do in Page_PreRender.

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

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

发布评论

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

评论(3

心安伴我暖 2024-09-26 03:32:31

某些事件在 Page_Load 事件之后触发。有些是明显的,而另一些则不是。这是因为这些事件只能在重新创建之后(或者更确切地说是加载之后)执行。例如,如果您想注册一段客户端脚本,那么最好的地方可能是Page_Render。或者,您可能有目的地在 Page_Load 中调用以验证控件的 Page.validate 方法可以理解为已在渲染阶段中调用。

同样,某些其他控件仅在其关联控件准备好呈现时才起作用(完全针对用户的浏览器进行处理)。

Certain events are triggered after the Page_Load event. Some are apparent while others are not. This is because these events can only perform after the recreation (or rather after loading). For eg, If you wish to register a piece of client script, The good place could be Page_Render. Or the Page.validate method which you might purposefully call in the Page_Load to validate controls can be understood to be already called in the render stages.

Likewise certain others which only work when their associated controls are ready to be rendered (fully treated for a user's browser).

七堇年 2024-09-26 03:32:31

这不是您只能在 Page_PreRender 中做什么的问题,控件之间可能存在依赖关系,只有在 Page_Load 中处理完所有事件后,这些依赖关系才会变得明显。

It's not a question of what you can only do in Page_PreRender, there may be dependencies between your controls that only become apparent after all the events have been processed in Page_Load.

时光与爱终年不遇 2024-09-26 03:32:31

Page_PreRender 事件可用于指定对控件的 html 呈现的更改。 Load 和 PreRender 之间的一个重要区别是 Load 在触发控件事件之前运行,而 PreRender 在控件事件之后调用。因此,如果您想要根据控件事件代码更改输出html,您可以在 PreRender 事件中执行此操作。

另一个需要注意的重要事项是 Page 对象在 Page 对象上引发 PreRender 事件,然后递归地对每个子控件执行相同的操作。各个控件的 PreRender 事件发生在页面的 PreRender 事件之后。因此,这使您可以更好地了解如何修改特定控件的呈现。这在 Page_Load 中是不可能的。

如果您使用自定义/用户控件(其中控件的呈现由您完成),也可以使用它。

Page_PreRender event can be used to specify changes to the html rendering for your controls. One important difference between Load and PreRender is that Load runs before your control events are fired and PreRender is called after the control events. So, if on the basis of your control events code you want to change the output html, you can do so in the PreRender event.

Another important thing to note is that the Page object raises the PreRender event on the Page object, and then recursively does the same for each child control. The PreRender event of individual controls occurs after the PreRender event of the page. So, this gives you added leverage on how to modify the rendering of a particular control. This is not possible in Page_Load.

It may also be used if you're using custom/user controls where the rendering of the control is done by you.

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