Web用户控件的Controls.Add()不会实现其子控件 [英] Controls.Add() for a Web User Control doesn't instansiate its child controls

查看:69
本文介绍了Web用户控件的Controls.Add()不会实现其子控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将描述问题的最简单情况。


我有一个简单的Web用户控件,它只包含:< div id =" bla"

runat =" server" /。

我动态地将它添加到页面中,执行:


SomeCtl ctl = new SomeCtl();

this .Controls.Add(ctl);


当然要应用一个<%@ Register%标签,以便将页面上的SomeCtl称为

是否有一些更优雅的方式?)。


我覆盖了控件的OnPreRender,为了根据某些属性写一些东西到

DIV控件,但我不能(在

事件代码中)访问DIV。它等于null。

问题当然只发生在我的用户控件(SomeCtl)动态添加到页面上时,而不是当我放置用户控件时在设计时页面内直接



提前感谢您的帮助,

Ofer

解决方案

" Ofer Zelig" < of ** @ newsgroups.nospamwrote in message

news:eD ************** @ TK2MSFTNGP04.phx.gbl ...
< blockquote class =post_quotes>
当然只有当我的用户控件(SomeCtl)被动态地添加到页面时才会出现问题,而不是当我直接放置用户控件时



您在哪种方法中添加控件...?


如果您在Page_Load中执行此操作,尝试在Page_Init中执行此操作...


该用户控件的主机本身就是一个控件(在这种情况下,一个

"常规,设计模式放置一个。

我不能把它放在主机控件的Page_Init中,因为代码必须

访问控件的属性尚未在其Page_Load

活动中提供......

" Mark Rae" < ma ** @ markNOSPAMrae.com写信息

news:ek ************** @ TK2MSFTNGP04.phx.gbl ...
< blockquote class =post_quotes>
" Ofer Zelig" < of ** @ newsgroups.nospamwrote in message

news:eD ************** @ TK2MSFTNGP04.phx.gbl ...
< blockquote class =post_quotes>
当然只有当我的用户控件(SomeCtl)被动态地添加到页面时才会出现问题,而不是当我直接放置用户控件时



您在哪种方法中添加控件...?


如果您在Page_Load中执行此操作,尝试在Page_Init中进行操作......



" Ofer Zelig" < of ** @newsgroups.nospamwrote in message

news:ek ************** @ TK2MSFTNGP04.phx.gbl ...
< blockquote class =post_quotes>
该用户控件的主机是一个控件本身(在这种情况下,

常规,设计模式放置一个)。



OK。


我不能把它放在主机控件的Page_Init中,因为代码必须

访问控件的属性,这些属性在其Page_Load

事件中尚未提供...



在这种情况下,你可能会发现你需要在这里重新考虑你的逻辑

位,因为我很确定问题是动态创建了

控制在整个页面循环中发生得太晚了...


我已经多次使用动态创建的控件...创建它们
$ b $在Page_Load中,他们有时会工作......在Page_Init或

Page_PreInit中创建它们,它们始终有效......


I''ll describe the simplest situation of the problem.

I have a simple Web User Control which only contains a: <div id="bla"
runat="server" /.
I dynamically add it to a page, by performing:

SomeCtl ctl = new SomeCtl();
this.Controls.Add(ctl);

and of course apply a <% @ Register %tag in order to make SomeCtl known to
the page (is there some more elegant way?).

I override OnPreRender of the control, in order to write some stuff to the
DIV according to some property of the control, but I can''t (within the
event code) access the DIV. It is equal to null.
The issue happens of course only when my user control (SomeCtl) is added
dynamically to the page, and not when I place the user control directly
within the page at design time.
Thanks in advance for your help,
Ofer

解决方案

"Ofer Zelig" <of**@newsgroups.nospamwrote in message
news:eD**************@TK2MSFTNGP04.phx.gbl...

The issue happens of course only when my user control (SomeCtl) is added
dynamically to the page, and not when I place the user control directly
within the page at design time.

In which method are you adding the control...?

If you''re doing it in Page_Load, try doing it in Page_Init instead...


The host of that user control is a control itself (in this case, a
"regular", design-mode placed one).
I can''t put it in Page_Init of the host control, because the code must
access properties of the control which are not available in its Page_Load
event yet...
"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:ek**************@TK2MSFTNGP04.phx.gbl...

"Ofer Zelig" <of**@newsgroups.nospamwrote in message
news:eD**************@TK2MSFTNGP04.phx.gbl...

The issue happens of course only when my user control (SomeCtl) is added
dynamically to the page, and not when I place the user control directly
within the page at design time.


In which method are you adding the control...?

If you''re doing it in Page_Load, try doing it in Page_Init instead...



"Ofer Zelig" <of**@newsgroups.nospamwrote in message
news:ek**************@TK2MSFTNGP04.phx.gbl...

The host of that user control is a control itself (in this case, a
"regular", design-mode placed one).

OK.

I can''t put it in Page_Init of the host control, because the code must
access properties of the control which are not available in its Page_Load
event yet...

In which case, you might find that you need to rethink your logic here a
bit, as I''m pretty sure the problem is that the dynamic creation of the
control is occurring too late in the overall page cycle...

I''ve seen this many times with dynamically created controls... Create them
in Page_Load, they sometimes work... Create them in Page_Init or
Page_PreInit, they always work...


这篇关于Web用户控件的Controls.Add()不会实现其子控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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