Setup visibility of controls programmatically

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
 
E

Edward

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
 
H

Hans Kesting

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top