Asp.Net UserControls 能否知道它们是否被“延迟”添加?到控制层次?
我有一个用户控件,它使用标准 if(!IsPostBack){//initialize myself}
范例,以避免在回发期间重新进行初始化(因此,用更少的 DB 命中换取更多的 ViewState 使用)。这种方法在大多数情况下都对我很有用,但有一个地方我想在回发期间“后期”将此控件添加到控件层次结构中。
当然,这会导致初始化逻辑失败,并且控件呈现为未初始化状态。
由于 !IsPostBack
没有切断它,我应该使用什么保护来确定是否应该初始化?我可以在 LoadViewState 期间设置一个标志,但这似乎有点黑客。我想找到的是仅当控件首次添加到控件层次结构时才会发生的某些条件,并对此进行关键。这样的条件存在吗?
[编辑] 包含页面的伪代码示例如下:
protected void Page_Prerender(object sender, EventArgs e)
{
Controls.Add(LoadControl("some_control.ascx"));
}
Is there a way for some_control
to Know it's was was displayed later?
I have a user control that uses the standard if(!IsPostBack){//initialize myself}
paradigm to avoid re-doing initialization during postbacks (so, trading fewer DB hits for increased ViewState usage). That approach serves me well most of the time but there's one place where I want to add this control to the control hierarchy 'late', during a postback.
This, of course, causes the initialization logic to fail, and the control to be rendered in an uninitialized state.
What guard should I be using to determine whether I should initialize, since !IsPostBack
isn't cutting it? I could set a flag during LoadViewState
, but that seems a bit hackish. What I'd like to find is some condition that only happens when a control is first added to the control hierarchy, and key on that. Does such a condition exist?
[edit] Sample pseudocode follows for the containing page:
protected void Page_Prerender(object sender, EventArgs e)
{
Controls.Add(LoadControl("some_control.ascx"));
}
Is there a way for some_control
to know it's been added late?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不能使用构造函数来初始化子控件吗? (或创建一个Initialize方法)然后您可以控制控件何时初始化。
Can't you use the constructor to initialize your child controls? (or create a Initialize method) Then you control when the control is initialized.
也许这会帮助您理解您的问题:
“……如果在事件处理程序中创建控件并将其动态添加到控件树中会怎样?在这种情况下,控件会进行追赶。一旦它被添加到控制树中,它就会开始执行其阶段,直到到达页面的当前阶段......”
更多信息如下:
http://weblogs.asp.net/vga/archive/2003/08/11/23498.aspx
Maybe this will help you to understand your problem:
“…what if a control is created in an event handler and dynamically added to the control tree? In that case, the control plays catch-up. As soon as it is added to the control tree, it starts to execute its phases until it reaches the current phase of the page…”
More informations here:
http://weblogs.asp.net/vga/archive/2003/08/11/23498.aspx
实际上并没有“第一次”添加控件的概念,因为请记住,每次请求页面时,都会创建一个新页面对象,其中包含所有新< /strong> 控制实例。以前创建的控件实例不会添加到新的页面对象中。
为什么初始化逻辑失败?也许如果您发布该代码,我们可以提出一些建议 - 似乎不一定必须如此。
There isn't really a concept of adding a control for the "first" time, because remember that every single time you request a page, a new page object is created, with all new control instances. Previously created control instances aren't being added to your new page object.
Why does the initialization logic fail? Perhaps if you posted that code we could suggest something - it doesn't seem like that should necessarily have to be the case.
进一步的搜索并没有让我找到这个问题的通用解决方案。我最终做的是在
Page_LoadViewState
中设置一个标志,该标志抑制控件的初始化 - 实际上与使用!IsPostBack
保护初始化相同,但更精确一些。Further searches did not lead me to a general solution to this problem. What I ended up doing was setting a flag in
Page_LoadViewState
which suppressed the control's initialization - effectively the same thing as guarding the initialization with!IsPostBack
, but a bit more precise.