Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does my usercontrol keep resetting it's visible property to false?

Tags:

c#

asp.net

Ok, I have a usercontrol on my page.

On the page, the visible property is set to false.

On the OnPreRender event, I set the visible property to true.

It runs the line of code, but does not actually change anything. (so visible remains at false)

This exact same method works across every other control, and there is nothing special about this control.

Any ideas??

like image 366
tim Avatar asked Dec 23 '22 10:12

tim


2 Answers

Check for the visible property on any controls containing this control.

Setting Visible=True does not mean that Visible==True, it will still return False if a parent control is False.

Other than that though, you may need to post some examples of your code in order for anyone to help track down what the problem may be.

like image 186
Robin Day Avatar answered Jan 18 '23 23:01

Robin Day


I faced the same problem and... yes the problem is the parents not be visible. So I made this little peace of code to solve the problem:

  public static void ForceVisibleState(Control control, bool visible)
  {
     if (!visible)
     {
        control.Visible = false;
     }
     else
     {
        // Must set all parents to 'visible = true'
        List<Control> parents = new List<Control>();
        while (control != null &&
               !control.Visible)
        {
           parents.Insert(0, control);
           control = control.Parent;
        }
        foreach(Control parent in parents)
        {
           parent.Visible = true;
        } 
     }
  }
like image 24
Cabbi Avatar answered Jan 18 '23 23:01

Cabbi