简体   繁体   中英

ASP.NET Not display user control C#

Say I have a user control (.ascx) on a .aspx page. In the code behind on the .ascx is there a way to tell itself to not load if a certain condition was met?

I don't want to just not display this control by javascript or css, I need to do it in the codebehind.

Any thoughts?

Most controls have a Visible property.

In your code-behind set this to False in order for it to not display:

myCtrl.Visible = false;
control.Visible = false;

Try:

this.Visible = false;

From within your ascx.cs codebehind file.

I would suggest to put the condition in the 'aspx' page that is loading the user control. So if you are calling LoadControl, you would surround it with the condition to make it available or otherwise.

You can also add a property say 'IsVisible' within the usercontrol and set visibility like others have suggested.

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