Hiding labels and textboxs in formview templates according to role

A

Ann

I have a formview with templates. I want to hide certain labels or textboxes
according to the user's role membership. I can do this in a sub routine as
long as it's not inside a template, is it possible to do this inside a
formview template?

Some code sample that does not work: (passphrase is the name of the label)

Protected Sub passPhrase_Init(ByVal sender As Object, ByVal e As
System.EventArgs)
If (Roles.IsUserInRole("networkServices")) Then
FormView1.itemtemplate.passphrase.visible = True

Else
FormView1.itemtemplate.passphrase.visible = False
End If
End Sub

Thank you for your time.

Ann
 
G

Guest

I have a formview with templates. I want to hide certain labels or textboxes
according to the user's role membership. I can do this in a sub routine as
long as it's not inside a template, is it possible to do this inside a
formview template?

Some code sample that does not work: (passphrase is the name of the label)

Protected Sub passPhrase_Init(ByVal sender As Object, ByVal e As
System.EventArgs)
If (Roles.IsUserInRole("networkServices")) Then
FormView1.itemtemplate.passphrase.visible = True

Else
FormView1.itemtemplate.passphrase.visible = False
End If
End Sub

Thank you for your time.

Ann

Have you added the event handler delegate for the init event?

<asp:Label ID="passPhrase" runat="server" OnInit="passPhrase_Init" />
 
G

Guest

Yes, I have added the event handler. The problem is it doesn't like my
syntax that I'm using, and I don't know what the correct syntax should be for
the path.

It doesn't like "FormView1.itemtemplate.passphrase.visible"
I get this error: BC30456: 'passphrase' is not a member of
'System.Web.UI.ITemplate'.

I think, it's simple then

Try something like

Protected Sub passPhrase_Init(ByVal sender As Object, ByVal e As
System.EventArgs)

Dim x As Label = CType(sender, Label)
If (Roles.IsUserInRole("networkServices")) Then
x.visible = True
Else
x.visible = False
End If
End Sub

You can also try to use

<asp:Label ... visible=<% #IsAdmin() %>

Public Sub IsAdmin() as Boolean

If (Roles.IsUserInRole("networkServices")) Then
Return True
Else
Return False
End If

End Sub
 
D

Dominick Baier

You have to use the FindControl method on the template. Add a trace="on"
page directive to get a better understanding of the contol nesting on the
page.
 
A

Ann

Yes, that worked! Thanks

Anon User said:
I think, it's simple then

Try something like

Protected Sub passPhrase_Init(ByVal sender As Object, ByVal e As
System.EventArgs)

Dim x As Label = CType(sender, Label)
If (Roles.IsUserInRole("networkServices")) Then
x.visible = True
Else
x.visible = False
End If
End Sub

You can also try to use

<asp:Label ... visible=<% #IsAdmin() %>

Public Sub IsAdmin() as Boolean

If (Roles.IsUserInRole("networkServices")) Then
Return True
Else
Return False
End If

End Sub
 
A

Ann

Thank you, I'll try this also.

Dominick Baier said:
You have to use the FindControl method on the template. Add a trace="on"
page directive to get a better understanding of the contol nesting on the
page.
 
D

Dominick Baier

you should be aware that even if a label/textbox whatever is invisible -
when you add data to it it will end up in viewstate. In addition to not showing
the control - it should also not contain data.
 

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

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top