Setup visibility of controls programmatically

  • Thread starter Thread starter Michel Couche
  • Start date Start date
M

Michel Couche

Hello!

I am working on an on-line ASP.Net library.
I would like to set up the visibility of some controls programmatically.
My approach is to give an "admin_" prefix to some controls and to define
their visibility by the procedure below ....

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
.....
Call SetUpVisibility_AdminControls(False)
....
End sub


....
....
Sub SetUpVisibility_AdminControls(ByVal Adminlevel As Boolean)
Dim obj_Control As Control
For Each obj_Control In Page.Controls
If Left(obj_Control.ID, 6) = "admin_" Then
Page.FindControl(obj_Control.ID)
obj_Control.Visible = Adminlevel
End If
Next
End Sub

For reason (...), this just does not work and, whatever the value of the
boolean "AdminLevel", the controls are always visible.

Thanks in advance for your support.

Michel
 
Michel,

Are you sure that the controls have Page as their direct parent , if
your control is in some placeholder like Table, usercontrol, your code
won't fulfill your requirement.

your can do recursive search. Hope it's the key. If not, hope you can
solve it with others' help.

Good luck!

Edward
 
Michel Couche said:
Hello!

I am working on an on-line ASP.Net library.
I would like to set up the visibility of some controls programmatically.
My approach is to give an "admin_" prefix to some controls and to define
their visibility by the procedure below ....

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
....
Call SetUpVisibility_AdminControls(False)
...
End sub


...
...
Sub SetUpVisibility_AdminControls(ByVal Adminlevel As Boolean)
Dim obj_Control As Control
For Each obj_Control In Page.Controls
If Left(obj_Control.ID, 6) = "admin_" Then
Page.FindControl(obj_Control.ID)
obj_Control.Visible = Adminlevel
End If
Next
End Sub

For reason (...), this just does not work and, whatever the value of the
boolean "AdminLevel", the controls are always visible.

Thanks in advance for your support.

Michel

You don't need that "Page.FindControl" in there, you already *have*
a control. Plus this is a function and you ignore any result!
As Edward replied, you need to do a recursive search.

Hans Kesting
 
Back
Top