user1342164
user1342164

Reputation: 1454

Listbox.items.count is always 0?

I am trying to determine if a list box has any text in it. I tried using a custom validator and the code below always has a result of 0 or false either when there is text or no text in the list box? How can I properly determine if there is text in a list box?

 Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
        args.IsValid = Listbox.Items.Count > 0
    End Sub

Private Sub PopulateListBox()


        If NameTextBox.Text = "" Then

        Else
            ' Get value from text box
            Dim textBoxValue As String = Me.NameTextBox.Text

            ' Create new item to add to list box
            Dim newItem As New ListItem(textBoxValue)

            ' Add item to list box and set selected index
            Listbox.Items.Add(newItem)
            Listbox.SelectedIndex = Listbox.Items.Count - 1



            End If

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

    If IsPostBack Then
        ' Put call here to populate the listbox results from autocomplete extender selection
        PopulateListBox()
    End If

End Sub

Upvotes: 1

Views: 996

Answers (1)

Brian Mains
Brian Mains

Reputation: 50728

By chance, is this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If IsPostBack Then
        ' Put call here to populate the listbox results from autocomplete extender selection
        PopulateListBox()
    End If
End Sub

Is the IF statement supposed to be instead:

    If Not IsPostBack Then

You currently have it not binding the first time, but everytime after.

Upvotes: 1

Related Questions