无法从共享方法引用类的实例成员

时间:2014-11-09 22:31:15

标签: asp.net vb.net

如何获得Protected Sub之外的公共共享功能,使用受保护子内的值将postBack复制到同一网页。回发回复有效,但函数查询失败(第44行Char 17“fqdom = dom&”.forest.local“)

Imports System
Imports System.IO
Imports System.DirectoryServices
Imports System.DirectoryServices.AccountManagement
Imports System.DirectoryServices.ActiveDirectory

Partial Class _Default 
 Inherits System.Web.UI.Page

Dim dom As String
Dim Group1 As String
Dim Group2 As String
Dim usrname As String
Dim fqdom As String
Dim netdom As String

Private Function GetDataFromArrayList() As ArrayList
    Dim DomainList As New ArrayList()
    DomainList.Add(New ListItem("d1", "dom1"))
    DomainList.Add(New ListItem("d2", "dom2"))
    Return DomainList
End Function

Protected Sub Selection_Changed(ByVal sender As Object, _
   ByVal e As System.EventArgs) Handles Me.Load

    If Not Page.IsPostBack Then
        For Each item As ListItem In GetDataFromArrayList()
            DropDownList1.Items.Add(item)
        Next
    End If
End Sub

Public Shared Function GetGroups() As ArrayList
    Dim groupList As New ArrayList()
    Dim usrname As String
    Dim fqdom As String
    'Dim netdom As String
    Dim groupCheck As String
    fqdom = dom & ".forest.local"

    Dim entry As System.DirectoryServices.DirectoryEntry
    Dim searcher As System.DirectoryServices.DirectorySearcher
    Dim result As System.DirectoryServices.SearchResult

    Try
        entry = New System.DirectoryServices.DirectoryEntry("LDAP://" & fqdom)
        searcher = New DirectorySearcher()
        searcher.SearchRoot = entry
        searcher.Filter = "(samAccountName=" & usrname & ")"
        searcher.PropertiesToLoad.Add("memberOf")
        result = searcher.FindOne()
        Dim groupCount As Integer = result.Properties("memberOf").Count
        For groupCounter As Integer = 0 To groupCount - 1
            groupCheck = CStr(result.Properties("memberOf")(groupCounter))
            groupCheck = groupCheck.Remove(groupCheck.LastIndexOf(",CN="))
            groupCheck = groupCheck.Replace("CN=", "")
            groupList.Add(groupCheck)
        Next groupCounter

    Catch ex As Exception

    End Try

    Return groupList
End Function


Protected Sub Button1_Click(ByVal sender As Object, _  
   ByVal e As System.EventArgs) Handles Me.Load

    If IsPostBack Then

        Dim name As Boolean = False

        If Not TextBox1.Text = String.Empty Then
            name = True
        End If


        If name = False Then
            StatusLabel.Text = "Update Status: Please Enter Name"

        ElseIf name = True Then


            Group1 = "groupb1"
            Group2 = "groupb2"

            Try
                form1.Visible = False
                Dim groups As New ArrayList()
                groups = GetGroups()
                Dim group As String
                For Each group In groups
                    'NameLabel.Text = group

                    If (group.Contains(Group1)) Then
                        Group1.Text = "User: " & usrname & " is in group1"
                    End If

                    If (group.Contains(Group2)) Then
                        Group1.Text = "User: " & usrname & " is in group2"
                    End If
                Next

                fqdn.Text = "Domain:  " & dom & ".forest.local"
                NameLabel.Text = "User: " & usrname

            Catch ex As Exception

            End Try

        Else
            StatusLabel.Text = "Upload status: Error Please Retry later"
        End If
    End If
End Sub
End Class 

1 个答案:

答案 0 :(得分:6)

从方法中删除Shared keyword,因此请替换

Public Shared Function GetGroups() As ArrayList

Public Function GetGroups() As ArrayList

您无法在dom方法中使用Shared等实例变量。

您还可以制作这些字段Shared。但这在ASP.NET中并不是一个好主意,因为它可能导致锁定和并发问题,并且每个请求共享相同的值(即使是不同的用户)。

也没有必要,因为你想从页面方法(按钮点击)中使用该方法,所以无论如何你都需要一个页面实例。

如果您需要在回发后保留值,则可以使用不同的方式,例如使用ViewState,Session或HiddenField。