列表框未正确选择我选择的项目 [英] Listbox not correctly picking the item I am choosing

查看:52
本文介绍了列表框未正确选择我选择的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

列表框是从数据库中的表中填充的,它有10个以上的项目。此列表框中的某些项目未正确突出显示/正确选择(第1,第2,第3个正确突出显示,第4个,第5个不是,第6个是,7th Not .... etc)。

选择项目后,它们会显示在同一页面上的标签中......除了没有正确选择的项目外,一切正常。任何帮助将不胜感激!



图片链接: http:/ /i.imgur.com/Nmr1gG2.png?1 [ ^ ]



我的尝试:



这是列表框代码:

The listbox is populated from a table in the database,it has more than 10 items.Some items in this listbox aren't being highlighted/chosen correctly(1st,2nd,3rd highlighted correctly,4th,5th Not,6th Yes,7th Not ....etc).
After choosing the item(s) they are displayed in a Label on the same page....everything works correctly except the items not being chosen correctly part. Any help will be appreciated!

Image link : http://i.imgur.com/Nmr1gG2.png?1[^]

What I have tried:

This is the listbox code:

    Sub FillItemsList()
        Dim connectionString As String = "Data Source=.;Initial Catalog=Shop;integrated security=true"
        Dim dt As New DataTable()

        Using connection As New SqlConnection(connectionString)
            Dim sql As String = "SELECT PhoneName,PhonePrice FROM SmartPhones"
            Using adapter As New SqlDataAdapter(sql, connection)
                adapter.Fill(dt)
            End Using
        End Using

        ListBox1.DataSource = dt
        ListBox1.DataTextField = "PhoneName"
        ListBox1.DataValueField = "PhonePrice"
        ListBox1.DataBind()

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

        If Not Page.IsPostBack Then
            FillItemsList()
        End If
        
    End Sub


    Protected 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 = sum + CInt(CurrentItem.Value)
            Items1 = Items1 + " | " + CStr(CurrentItem.Text)
        Next
        Label3.Text = Items1
        Label1.Text = sum


    End Sub





列表框asp代码:



Listbox asp code:

<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" autopostback="true"</asp:ListBox>

推荐答案

根据说明在注释中,您在 PhonePrice 列中有多个具有相同值的记录。



当表单回发到服务器时,只发送所选项目的值;服务器不知道您选择的具有相同值的多个项目中的哪个。结果,它选择具有指定值的第一个项目。



不,这不是你可以改变的东西。这就是HTML表单的工作方式。



您需要确保列表中的每个项目都具有唯一值。尝试使用 SmartPhones 表中的主键。
Based on the description in the comments, you have multiple records with the same value in the PhonePrice column.

When the form posts back to the server, only the value of the selected item(s) is sent; the server has no idea which of the multiple items with the same value you have selected. As a result, it selects the first item with the specified value.

And no, this is not something you can change. It's just how HTML forms work.

You'll need to make sure each item in your list has a unique value. Try using the primary key from your SmartPhones table.


这篇关于列表框未正确选择我选择的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆