0

I want to retrieve the values of the current selected items in the listbox and whenever the user pick more items,the sum of price of these items will be displayed in a Label ex: the user picks 2 phone names from the listbox and the sum of their price will be displayed in the label (It is working for only the first item the user chooses but ignores everything else selected and it doen't work when the user select an item it only displays the price when i press a button) ,Here is what i tried :

Partial Class PickItems
Inherits System.Web.UI.Page

Dim sum As Integer = 0
Protected Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged

    sum = sum + CInt(ListBox1.SelectedValue)
    Label1.Text = sum.ToString()


End Sub

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Label2.Text = Request.QueryString("Name").ToString()


End Sub End Class

Here is the Listbox code <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" DataSourceID="SqlDataSource1" DataTextField="PhoneName" DataValueField="PhonePrice"></asp:ListBox>

And here is how the page looks like: enter image description here I will also need to move the selected items from this page to the next page(same as i did with the Request.QueryString("Name....) Any help will be appreciated!

1 Answer 1

1

If you want a sum, you need to loop into each of the selected indices to calculate the sum.

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    sum = 0 'reset sum to 0
    For Each I As Integer In ListBox1.GetSelectedIndices
        Dim CurrentItem As ListItem = ListBox1.Items(I)
        sum += CInt(CurrentItem.Value) 
    Next
End Sub

Please note that for the SelectedIndexChanged event to work, you will need to set the AutoPostback attribute to true for your listbox.

AutoPostBack="True"
Sign up to request clarification or add additional context in comments.

5 Comments

I added 'Label1.Text = sum' after Next but the sum is 0 whatever item is chosen...How can i get it to show the correct sum?
i also added the code to onclick button event because the selectedindexchanged is not working and updating the label,only pressing the button will show the sum in Label1.
If you want it to execute when listbox selection is changed, you need to add AutoPostBack="True" in your listbox code so the event is called. Otherwise, SelectedIndexChanged is not called.
True but the page will refresh when i click on any item now ,so i can't choose more than 1 item.
The page refresh but you do not loose selected items and can therefore select multiples one. The more clean way to do it would be to use webapi request to the server than update the value with Javascript. That way, you avoid a page refresh and a button click and you get the updated value to your label. That is the way I would go. You can see an example here: techfunda.com/howto/304/update-record-using-web-api

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.