[英]ASP.NET custom control cancel rendering
I have a custom ASP.NET control that derives from Panel. 我有一个从Panel派生的自定义ASP.NET控件。 It has a default constructor and the RenderBeginTag, RenderContents and RenderEndTag overrides. 它具有默认构造函数,并且RenderBeginTag,RenderContents和RenderEndTag覆盖。
Now in the constructor i want to check a few properties and in a certain case i want to prevent /cancel the complete rendering of the control. 现在在构造函数中,我想检查一些属性,在某些情况下,我想防止/取消控件的完整呈现。
What would be the easiest way to do this? 最简单的方法是什么? Preferably just right there in that constructor. 最好就在那个构造函数中。 Right now i have added a small check to all override methods, but i'm sure this can be done a bit smarter. 现在,我已经为所有覆盖方法添加了一个小检查,但是我敢肯定这可以做得更聪明。
Also you could override the WebControl.Render
method to perform your check - this way you don't have to check in 3 different methods. 同样,您可以重写WebControl.Render
方法来执行检查-这样,您就不必检入3种不同的方法。
But a better solution would be to set this.Visible = false
- this prevents the render methods from being called and also prevents PreRender
events from being raised (they by design should be only raised on visible controls). 但是更好的解决方案是设置this.Visible = false
这样可以防止调用render方法,并且还可以防止引发PreRender
事件(它们应设计为仅在可见控件上引发)。
I would override the Visible
property and add the check there: 我将覆盖Visible
属性并在其中添加检查:
public override bool Visible
{
get
{
var b = base.Visible;
if (!b || this.ControlShouldNotBeRendered())
return false;
return true;
}
set
{
base.Visible = value;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.