Hiding labels and textboxs in formview templates according to role

Discussion in 'ASP .Net Security' started by Ann, May 14, 2007.

  1. Ann

    Ann 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
     
    Ann, May 14, 2007
    #1
    1. Advertising

  2. On May 14, 6:55 pm, Ann <> wrote:
    > 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" />
     
    Alexey Smirnov, May 14, 2007
    #2
    1. Advertising

  3. Re: Hiding labels and textboxs in formview templates according to

    On May 14, 10:52 pm, Ann <> wrote:
    > 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
     
    Alexey Smirnov, May 14, 2007
    #3
  4. Re: Hiding labels and textboxs in formview templates according to

    On May 14, 10:59 pm, Alexey Smirnov <> wrote:
    > Public Sub IsAdmin() as Boolean


    Public FUNCTION IsAdmin() as Boolean
     
    Alexey Smirnov, May 14, 2007
    #4
  5. 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.

    -----
    Dominick Baier (http://www.leastprivilege.com)

    Developing More Secure Microsoft ASP.NET 2.0 Applications (http://www.microsoft.com/mspress/books/9989.asp)

    > 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
    >
     
    Dominick Baier, May 14, 2007
    #5
  6. Ann

    Ann Guest

    Re: Hiding labels and textboxs in formview templates according to

    Yes, that worked! Thanks

    "Alexey Smirnov" wrote:

    > On May 14, 10:52 pm, Ann <> wrote:
    > > 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
    >
    >
     
    Ann, May 14, 2007
    #6
  7. Ann

    Ann Guest

    Re: Hiding labels and textboxs in formview templates according to

    Thank you, I'll try this also.

    "Dominick Baier" wrote:

    > 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.
    >
    > -----
    > Dominick Baier (http://www.leastprivilege.com)
    >
    > Developing More Secure Microsoft ASP.NET 2.0 Applications (http://www.microsoft.com/mspress/books/9989.asp)
    >
    > > 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
    > >

    >
    >
    >
     
    Ann, May 14, 2007
    #7
  8. Re: Hiding labels and textboxs in formview templates according to

    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.

    -----
    Dominick Baier (http://www.leastprivilege.com)

    Developing More Secure Microsoft ASP.NET 2.0 Applications (http://www.microsoft.com/mspress/books/9989.asp)

    > Yes, that worked! Thanks
    >
    > "Alexey Smirnov" wrote:
    >
    >> On May 14, 10:52 pm, Ann <> wrote:
    >>
    >>> 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
    >>
     
    Dominick Baier, May 15, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lily
    Replies:
    1
    Views:
    1,492
    Teemu Keiski
    Dec 23, 2003
  2. JKop
    Replies:
    3
    Views:
    477
  3. Slim
    Replies:
    6
    Views:
    391
    Eliyahu Goldin
    Jul 31, 2006
  4. recover
    Replies:
    2
    Views:
    809
    recover
    Jul 25, 2006
  5. =?Utf-8?B?QW5u?=
    Replies:
    0
    Views:
    439
    =?Utf-8?B?QW5u?=
    May 12, 2007
Loading...

Share This Page