简体   繁体   中英

ASP.NET custom control cancel rendering

I have a custom ASP.NET control that derives from Panel. It has a default constructor and the RenderBeginTag, RenderContents and RenderEndTag overrides.

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.

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).

I would override the Visible property and add the check there:

public override bool Visible
{
    get
    {
        var b = base.Visible;
        if (!b || this.ControlShouldNotBeRendered())
            return false;
        return true;
    }

    set
    {
        base.Visible = value;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM