locked
How to set permissions on certain SQL data RRS feed

  • Question

  • User75903138 posted

    On the membership section of the club starter kit, I'm trying to place a code where only administrators can view the member phone number and address. How would I do that?

     Can anyone help me edit this code to make it work or have a better way of doing it:

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

    If User.Identity.IsAuthenticated Then

    addressLabel.Visible = Page.User.IsInRole("Administrators")

    End If

    End Sub

    Friday, February 1, 2008 10:28 AM

Answers

  • User75903138 posted

    I got it to work guys, Thanks! This is what I used:

     

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

    If Not User.IsInRole("Administrators") Then

    Dim ItemCount As Integer = DataList1.Items.Count

    Dim i As Integer

    For i = 0 To ItemCount - 1

    DataList1.Items.Item(i).FindControl("phonelabel").Visible = False

    DataList1.Items.Item(i).FindControl("addressLabel").Visible = False

    Next

    End If

    End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 8, 2008 9:52 AM

All replies

  • User1092940952 posted

    Would the LoginView control work for you?  It allows you to configure different outputs based on the role of the user.

    Saturday, February 2, 2008 12:09 AM
  • User2069888697 posted

    Here you go, of course make sure the default is set to Visible="False" on the aspx page or use an else in the code below.

    If User.IsInRole("Administrator") Then

    Label1.Visible = "True"

    End If

    Saturday, February 2, 2008 1:49 PM
  • User75903138 posted

    How would I set permission to this ID?

     

    <p>

    Phone:

    <asp:Label ID="phonelabel" runat="server" Text='<%# Eval("Phone")%>' />

    </p>

    Monday, February 4, 2008 10:08 AM
  • User2069888697 posted

    Change the lable to

    <asp:Label ID="phonelabel" runat="server" visible="false" Text='<%# Eval("Phone")%>' />

    In the page load use the previous code tro set, phonelabel.visible="true".

    Monday, February 4, 2008 4:35 PM
  • User75903138 posted

    This is what I got on the page load so far and it still doesn't work. It doesn't like the addressLabel because it hasn't been declare. Sorry I'm a noob.

     

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

    If User.IsInRole("Administrator") Then

    phonelabel.Visable = "true"

    End If

    End Sub

    Monday, February 4, 2008 4:57 PM
  • User1092940952 posted

     The code looks good to me which leads me to this - are you sure that whatever user account you're running under is a member of the Administrator role?

    Monday, February 4, 2008 7:49 PM
  • User75903138 posted

    Yes, but VS2008 doesn't like the phonelable, it says the varible is not declared.

    Tuesday, February 5, 2008 9:40 AM
  • User2069888697 posted

    The phonelabel is part of the datalist, therefore a child control.  I will have to check for sure but I believe you need to use

     datalist1.findcontrol("phonelabel").visible=false

    Wednesday, February 6, 2008 10:26 AM
  • User2069888697 posted

    This code works

     

            If Not User.IsInRole("Adimistrator") Then
                Dim ItemCount = DataList1.Items.Count
                Dim i As Integer
                For i = 0 To ItemCount - 1
                    DataList1.Items.Item(i).FindControl("phonelabel").Visible = False
                Next
            End If
     
    Wednesday, February 6, 2008 11:21 AM
  • User75903138 posted

    Is that code in VB or C#? If C# could you write that in VB?

    Thursday, February 7, 2008 10:13 AM
  • User1092940952 posted

    Is that code in VB or C#? If C# could you write that in VB?

     

    That was VB 

    Thursday, February 7, 2008 1:39 PM
  • User75903138 posted

    It needs a As clause after the ItemCount. Do I do it as a Object?

    Thursday, February 7, 2008 1:55 PM
  • User1092940952 posted

    It needs a As clause after the ItemCount. Do I do it as a Object?

     

    Do you mean something needs to be cast?  Doesn't look like it to me unless you see something I don't. 

    Thursday, February 7, 2008 2:46 PM
  • User2069888697 posted

     

    Converted using  http://labs.developerfusion.co.uk/convert/vb-to-csharp.aspx

    Unconfirmed.

    { 
        if (!User.IsInRole("Adimistrator")) { 
            object ItemCount = DataList1.Items.Count; 
            int i; 
            for (i = 0; i <= ItemCount - 1; i++) { 
                DataList1.Items.Item(i).FindControl("phonelabel").Visible = false; 
            } 
        } 
    } 
    
     
    Thursday, February 7, 2008 6:23 PM
  • User75903138 posted

    I got it to work guys, Thanks! This is what I used:

     

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

    If Not User.IsInRole("Administrators") Then

    Dim ItemCount As Integer = DataList1.Items.Count

    Dim i As Integer

    For i = 0 To ItemCount - 1

    DataList1.Items.Item(i).FindControl("phonelabel").Visible = False

    DataList1.Items.Item(i).FindControl("addressLabel").Visible = False

    Next

    End If

    End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 8, 2008 9:52 AM