views:

190

answers:

4

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?

+1  A: 

Try:

this.Visible = false;

From within your ascx.cs codebehind file.

Wim Hollebrandse
+2  A: 

Most controls have a Visible property.

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

myCtrl.Visible = false;
Oded
+2  A: 
control.Visible = false;
Nissan Fan
So in the code behind for the .ascx I can refer to itself and tell it not to be visible?
Brandon
Yes, you could do this.Visible = false;
Nissan Fan
A: 

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.

SoftwareGeek
I tried using this method, despite the studies was failing. In the end, I had to use the method visible = true/false @SoftwareGeek
Ph.E