ListBox Get DataValueField

2 posts, 0 answers
  1. Allan
    Allan avatar
    121 posts
    Member since:
    Jun 2008

    Posted 14 Oct 2011 Link to this post

    I have two listboxes on a form. One is populated using a SQLDataSource and the other is populated using a StoredProc.

    <telerik:RadListBox ID="rlb_ADGroups" runat="server" CheckBoxes="True" 
    DataKeyField="GroupName" DataSourceID="sds_ADGroups" DataTextField="GroupName" 
    DataValueField="GroupName" Height="200px" Width="400px">
    </telerik:RadListBox>
      
    <asp:SqlDataSource ID="sds_ADGroups" runat="server"
    ConnectionString="<%$ ConnectionStrings:IT_CentralConnectionString %>"       
    SelectCommand="SELECT [GroupName], [ADsPath] FROM [vw_AD_ADSI_Groups] ORDER BY [GroupName]">
    </asp:SqlDataSource>
      
      
      
      
    <telerik:RadListBox ID="rlb_MemberGroups" runat="server" CheckBoxes="True" 
    DataKeyField="groupName" DataTextField="groupName"
    DataValueField="groupName" Height="200px" Width="400px">
    </telerik:RadListBox>

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
      
            rlb_MemberGroups.DataSource = GetGroups()
            rlb_MemberGroups.DataBind()
      
        End Sub
      
      
        Private Function GetGroups() As DataTable
      
            'Dim UserName As String = Request.QueryString("UserName")
            Dim UserName As String = "abrowning"
      
            Dim connectionString As String = DirectCast(ConfigurationManager.ConnectionStrings("IT_CentralConnectionString").ConnectionString, String)
            Dim connection As New SqlConnection(connectionString)
            Dim command As New SqlCommand(connectionString, connection)
      
            command = New SqlCommand("procGetGroupUsers", connection)
            command.CommandType = CommandType.StoredProcedure
      
            command.Parameters.Add("@UserName", SqlDbType.VarChar).Value = UserName
      
            command.Connection.Open()
      
            Dim myDataAdapter As New SqlDataAdapter(command)
            Dim myDataSet As New DataSet
            Dim dtData As New DataTable
            myDataAdapter.Fill(myDataSet)
            Return myDataSet.Tables(0)
      
            command.Connection.Close()
      
        End Function


    I want to be able to add and remove a user to and from AD groups. When I add a user to groups and obtain the data from the List Box populated from the SQLDataSource, the user is added to the groups. I also know the values are pulling from the listbox because I place the values in a label so I can see them. (For testing). Here is the code for adding the user to the groups:

    Protected Sub btn_AddToGroup_Click(sender As Object, e As System.EventArgs) Handles btn_AddToGroup.Click
        Dim UserName As String = "abrowning"
        For Each item As RadListBoxItem In rlb_ADGroups.CheckedItems
            Dim ADGroup As String = item.Value.ToString()
            Label1.Text = ADGroup
            Using context = New PrincipalContext(ContextType.Domain, "tustin_nt")
                Dim group As GroupPrincipal = GroupPrincipal.FindByIdentity(context, ADGroup)
                If group IsNot Nothing Then
                    Using user = UserPrincipal.FindByIdentity(context, IdentityType.Name, UserName)
                        If UserName IsNot Nothing Then
                            group.Members.Add(context, IdentityType.UserPrincipalName, UserName + "@tusd.local")
                            group.Save()
                        End If
                    End Using
                End If
            End Using
        Next
    End Sub

    However, when I attempt the same process to remove a user, I am unable to obtain the values from the List Box that was populated using the StoredProc. I can tell no values are being obtained becasue the Label I attempt to populate stays empty. Here is my code to remove the user from groups:

    Protected Sub btn_RemoveFromGroup_Click(sender As Object, e As System.EventArgs) Handles btn_RemoveFromGroup.Click
           Dim username As String = "abrowning"
           For Each item As RadListBoxItem In rlb_MemberGroups.CheckedItems
               Dim ADGroup As String = item.Value.ToString()
               Label2.Text = ADGroup
               Using context = New PrincipalContext(ContextType.Domain, "TUSTIN_NT")
                   Dim group As GroupPrincipal = GroupPrincipal.FindByIdentity(context, ADGroup)
                   If group IsNot Nothing Then
                       Using user = UserPrincipal.FindByIdentity(context, IdentityType.Name, username)
                           If username IsNot Nothing Then
                               group.Members.Remove(context, IdentityType.UserPrincipalName, username + "@tusd.local")
                               group.Save()
                           End If
                       End Using
                   End If
               End Using
           Next
       End Sub


    I have set the DataKeyField, DataTextField, and the DataValueField in the rlb_MemberGroups List Box.

    Can someone please help explain why I am unable to retrieve the Checked value from one List Box but not the other?

    Thank you in advance.


  2. Allan
    Allan avatar
    121 posts
    Member since:
    Jun 2008

    Posted 17 Oct 2011 Link to this post

     

    Figured it out. Forgot:

    If Not IsPostBack Then

  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top