Can not get (Listbox.Selecteditem.Value)

Can not get (Listbox.Selecteditem.Value)
It gives error (Object reference not set to an instance of an object)

Here is aspx code

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_default" %>
<!DOCTYPE html>
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>

Here is the code behind

Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        ListBox1.Items.Add(New ListItem("saljas", "123"))
    End Sub
    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim xxx As String
'(the error is on the following line "Object reference not set to an instance of an object.")
       xxx = ListBox1.SelectedItem.Value  
'( note that (ListBox1.items count) works fine)
    End Sub
End Class

Help is appreciated
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Obadiah ChristopherCommented:
You need to add the check for IsPostback in ur page load event before you bind the listbox. here’s a c# example.


What is happening in your code is that the Listbox is being bound each time you click the button and you are losing the selecteditem value.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
saljasAuthor Commented:
do you mean

(if the page is posted back then do not add to list)
        If Page.IsPostBack Then Exit Sub      
        ListBox1.Items.Add(New ListItem("salah", "123"))


(if the page is posted back then add to list)
        If Page.IsPostBack Then        
            ListBox1.Items.Add(New ListItem("salah", "123"))
        End if
Obadiah ChristopherCommented:
If Not IsPostBack
            ListBox1.Items.Add(New ListItem("salah", "123"))
        End if 

Open in new window

saljasAuthor Commented:
The answer was what I am looking for
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.