ASP.NET RenderControl或RenderChildren失败 [英] ASP.NET RenderControl or RenderChildren fail
问题描述
我需要使用objChildControl.RenderControl或objControl.RenderChildren手动使我的子控件。但它看起来像这些方法都是不完整的。
I need to use objChildControl.RenderControl or objControl.RenderChildren to manually render my child controls. But it looks like these methods are incomplete.
我所有的子控件使用On preRender事件注册clientscript和客户端样式表(因为这些只能在prerender事件被创建)。
All my child controls use the OnPreRender event to register clientscript and client stylesheets (since these can only be created in the prerender event).
我有2个主要问题,传递当前System.Web.UI.Page对象对孩子的控制,并确保开preRender事件对这些子控件被解雇了。
I have 2 main issues, passing the current System.Web.UI.Page object to a child control and making sure the OnPreRender event is fired on these child controls.
看来,我不能用我的子控件的RenderControl方法,因为在preRender事件将不会被调用。
然而,我可以objChildControl.Page = Me.Page
It seems that I can't use the RenderControl method on my child controls since the OnPreRender event will not be called. I can however pass the Page object by objChildControl.Page = Me.Page
当我使用RenderChildren我无法通过Page对象,或可以吗?
我不知道当我使用RenderChildren开preRender事件甚至被称为
When I use RenderChildren I cannot pass the Page object, or can I? And i'm not sure if the OnPreRender event is even called when I use RenderChildren.
有些帮助将是AP preciated,因为我卡住;)
Some help would be appreciated, since i'm stuck ;)
更新
我找到了一种方法得到的结果,我需要,但它不是我想要的解决方案。
例如:
I found a way to get the result I need, but it is not the solution I want. Example:
code我想:
<wc:ParentControl id="objParent" runat="server" bla="etc">
<Content> <!-- This is an InnerProperty of the ParentControl --><DIV>bla bla bla bla.....<wc:SomeControl id="objSomeControl" runat="server" /><wc:3rdPartyControl id="obj3rdPartyControl" runat="server" /></DIV></Content>
</wc:ParentControl>
codeBehind:objParentControl.Content.RenderControl(作家)
CodeBehind: objParentControl.Content.RenderControl(Writer)
和则上述问题将开始。如何确保对内容中的所有孩子们在preRender会叫?
And then the issues mentioned above will begin. How to make sure that for all the children within Content the OnPreRender will be called?
code这不工作(但随后的RenderControl方法就是没用):
Code which does work (but then the RenderControl method is just useless):
<wc:ParentControl id="objParentControl" runat="server"></wc:ParentControl>
<wc:Content id="objContent" runat="server"><DIV>bla bla bla bla.....<wc:SomeControl id="objSomeControl" runat="server" /><wc:3rdPartyControl id="obj3rdPartyControl" runat="server" /></DIV></wc:Content>
然后,只需使用对RenderBeginTag和RenderEndTag的WC的:内容控制。
然后,在preRender事件被调用。
但我wan't通过使用InnerProperty嵌入内容,存入parentcontrol。
然后手工绘制由RenderControl或RenderChildren的childcontrols。
Then just use the RenderBeginTag and RenderEndTag of the wc:Content control. Then the OnPreRender event is called. But I wan't to embed the content into the parentcontrol by using an InnerProperty. And then manually rendering the childcontrols by RenderControl or RenderChildren.
推荐答案
我有一个类似的问题。我不知道这是否是您遇到了同样的问题,但我遇到的问题是,我有一个 ParseChildren(真)
我的容器控件的属性。因为ParseChildren是真实的,子控件会被放入一个属性,而不是到包含控件的子控件集合,并永远不会得到所谓的在preRender
功能
I had a similar issue. I'm not sure if it's the same issue you're experiencing, but the problem I was having was that I had a ParseChildren(true)
attribute on my container control. Because ParseChildren was true, the child controls would be put into a property, rather than into the containing control's child controls collection, and would never get their OnPreRender
function called.
我结束了覆盖在我的包含控制类,在这里我补充一切从我的分析收集到控制
集合。因为我反正重写的CreateChildControls
函数渲染
功能,我不担心Controls集合正在呈现时,我不想让他们成为控制。
I ended up overriding the CreateChildControls
function in my containing control class, where I added everything from my parsed collection to the Controls
collection. Because I'm overriding the Render
function anyway, I don't worry about the controls in the Controls collection being rendered when I didn't want them to be.
喜欢的东西如下:
[ParseChildren(true, "MyKids")]
public class Example : Control {
private ArrayList _kids = new ArrayList();
public ArrayList MyKids {
get { return _kids; }
set { _kids = value; }
}
protected override CreateChildControls() {
Controls.Clear();
foreach(Control c in _kids)
Controls.Add(c);
}
protected override Render(HtmlTextWriter writer) {
...
}
}
这篇关于ASP.NET RenderControl或RenderChildren失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!