修改下拉列表数据绑定中的用户列表

时间:2014-07-18 11:51:03

标签: asp.net vb.net

我使用下面的代码在下拉框中显示用户列表。如果用户被锁定或禁用,我该如何将其添加到列表中呢?

例如,如果我们有名字' John'在列表中...如果帐户被禁用,怎么可以更改它以显示“约翰 - 已禁用”#39;在列表中?

编辑:为每个循环添加一个后,如果只有一个帐户被禁用,则整个列表显示已禁用 - 不仅仅是针对该帐户。

Inherits System.Web.UI.Page

Private webUsers As MembershipUserCollection

Protected Sub Page_LoadComplete(sender As Object, e As System.EventArgs) Handles Me.LoadComplete

    webUsers = Membership.GetAllUsers()

    If Not IsPostBack Then

        UserName.DataSource = webUsers
        UserName.DataBind()

    End If

     For Each item As Object In UserName.Items
        If item Is IsApproved = False Then
            item.text = item.ToString & " - Disabled"
        End If
    Next

End Sub

3 个答案:

答案 0 :(得分:1)

您可能想要做的是修改webUsers集合中的基础对象。创建一个执行动态逻辑的新ReadOnly Property,并将该新属性用作下拉列表控件的DisplayText

如果没有看到有关webUsers集合定义的更多信息,很难给出一个具体的例子。

Public Class WebUser
    Public Property Name As String
    Public Property UserId As Integer
    Public Property IsApproved As Boolean

    Public ReadOnly Property DisplayName As String
        Get
            If Not IsApproved Then Return String.Format("{0} - Disabled", Name)
            Return Name
        End Get
    End Property
End Class

答案 1 :(得分:0)

试试这个......             item.text = item.ToString()+“ - Disabled”

答案 2 :(得分:0)

当然,您只需要使用正确的type而非Object,以便GetAllUsers()返回MemeberUserCollection,而不是使用Object MembershipUser

Inherits System.Web.UI.Page

Private webUsers As MembershipUserCollection

Protected Sub Page_LoadComplete(sender As Object, e As System.EventArgs) Handles Me.LoadComplete

    webUsers = Membership.GetAllUsers()

    If Not IsPostBack Then

        UserName.DataSource = webUsers
        UserName.DataBind()

    End If

     ' Option One - Loop through the webusers collection

     For Each item As MembershipUser In webUsers
        If item.IsApproved = False Then
             UserName.Items.FindByValue(item.UserName).Text += " - Disabled" 
        End If
    Next

    ' Option two - Loop through the ListItems of the list box
     For Each item As ListItem In UserName.Items
        If Membership.GetUser(item.Text).IsApproved = False Then
             item.Text += " - Disabled" 
        End If
    Next


End Sub