为什么这段代码在Page_PreRender()中写入,为什么不在page_Load()中写入 [英] why this code write here in Page_PreRender() and why not in page_Load() for example

查看:60
本文介绍了为什么这段代码在Page_PreRender()中写入,为什么不在page_Load()中写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hii,每个人



为什么这段代码写在Page_PreRender()

,为什么不在page_Load()例如

代码在这里:



  void  Page_PreRender( )
{
foreach (BaseValidator valControl in Page.Validators)
{
WebControl assControl =
(WebControl)Page.FindControl(valControl.ControlToValidate);
if (!valControl.IsValid)
assControl.BackColor = System.Drawing.Color.Yellow;
else
assControl.BackColor = System.Drawing.Color.White;
}
}

解决方案

在页面生命周期的这个阶段,代码位于Page_PreRender方法中,已创建所需的页面上的所有控件。对于Page_Load事件,这不是真的。



在这里查看更多信息

http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100)。 aspx [ ^ ]


这取决于您的要求。



页面加载:执行所有请求共有的操作,例如设置数据库查询。此时,树中的服务器控件被创建并初始化,状态被恢复,表单控件反映客户端数据。



Prerender:在呈现输出之前执行任何更新。可以保存在预渲染阶段对控件状态所做的任何更改,而在渲染阶段中进行的更改将丢失。



ASP.NET应用程序和页面生命周期 [ ^ ]

< a href =http://msdn.microsoft.com/en-us/library/ms178472.aspx> ASP.NET页面生命周期概述 [ ^ ]

控制执行生命周期 [ ^ ]

Differen介于page_init,page_load和page_prerender事件之间 [ ^ ]

页面事件 [ ^ ]


Hii,everyone

why this code write here in Page_PreRender()
and why not in page_Load() for example
code here :

void Page_PreRender()
{
foreach (BaseValidator valControl in Page.Validators)
{
WebControl assControl =
(WebControl)Page.FindControl(valControl.ControlToValidate);
if (!valControl.IsValid)
assControl.BackColor = System.Drawing.Color.Yellow;
else
assControl.BackColor = System.Drawing.Color.White;
}
}

解决方案

The code is in the Page_PreRender method as at this stage in the page lifecycle, all the controls on the page that are needed have been created. This isn''t true for the Page_Load event.

Have a look here for more information
http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx[^]


It depends on your requirements.

Page Load : Perform actions common to all requests, such as setting up a database query. At this point, server controls in the tree are created and initialized, the state is restored, and form controls reflect client-side data.

Prerender :Perform any updates before the output is rendered. Any changes made to the state of the control in the prerender phase can be saved, while changes made in the rendering phase are lost.

ASP.NET Application and Page Life Cycle[^]
ASP.NET Page Life Cycle Overview[^]
Control Execution Lifecycle[^]
Difference between page_init, page_load and page_prerender events[^]
Page Events[^]


这篇关于为什么这段代码在Page_PreRender()中写入,为什么不在page_Load()中写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆