How to get the text of the selected item in the listbox?

Asked

Viewed 954 times

1

My attempt below, is giving Object Reference, why? how to solve? inserir a descrição da imagem aqui

  • I am selecting yes! To popular I did the following: lbPerfis.Datasource = Roles.Getallroles() lbPerfis.Databind()

3 answers

1

Your problem is in the Page_Load, because every time a postback is made its method of Page_Load is called and is always recharging your data.

To avoid reloading data on PostBack just doing that:

Protected Sub Page_Load(ByVal sender as Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        PreencheListaUsuarios() 
        PreencheListaPerfis()
    End If
End Sub

That way when he gets on btnDeletarPerfil_click listbox data will be normal. :)

  • Corretooo, vlw man!!!

  • @Wellitonmeneguim If you solved your problem, mark the answer as the solution. :)

0

If (lbPerfis.Items.Count = 0) = False Then
    If (lbPerfis.SelectedItem Is Nothing) = False Then
         roles.DeleteRole(lbPerfis.SelectedItem.Text.ToString)
    End If
End If

-1

C# ////////////////////////////////

String value = Seu_listbox.Getitemtext(Seu_listbox .Selectedindex); //get index

String Value1 = Seu_listbox.Getitemtext(Seu_listbox.Text ); //get Text

Messagebox.Show(Value);

Messagebox.Show(Valor1);

VB.Net ///////////////////////////////////////

Dim Value As String = Seu_listbox.Getitemtext(Seu_listbox.Selectedindex)

Dim Valor1 As String = Seu_listbox.Getitemtext(Seu_listbox.Text)

Messagebox.Show(Value)

Messagebox.Show(Valor1)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.