|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Aug 2000
Messaggi: 1209
|
[ASP] Popolare e selezionare un item su ListBox()...
Su ASP.NET:
Ho un ListBox() che popolo nel seguente modo: Codice:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'popolo la ListBox Categorie
Dim ID As Integer
Dim Cat As String
conn.Open()
Dim sql As String = "SELECT * FROM Categorie"
Dim comm As New OleDbCommand(sql, conn)
Dim read As OleDbDataReader = comm.ExecuteReader
While read.Read()
ID = read("Id")
lsbCat.Items.Add(read("Cat"))
End While
conn.Close()
End Sub
Poi voglio selezionare un Item e fare popolare un altra ListBox() con l'Item selezionato dalla prima, e faccio nel segiente modo: Codice:
Private Sub lsbCat_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lsbCat.SelectedIndexChanged
'popolo la ListBox SubCategorie
Dim ID As Integer = lsbCat.SelectedItem.Value
Dim IDC As Integer
Dim IDS As Integer
Dim SubCat As String
conn.Open()
Dim sql As String = "SELECT * FROM SubCategorie WHERE IDC = " & ID & ""
Dim comm As New OleDbCommand(sql, conn)
Dim read As OleDbDataReader = comm.ExecuteReader
While read.Read()
IDS = read("Id")
IDC = read("IDC")
lsbSubCat.Items.Add(read("SubCat"))
End While
conn.Close()
End Sub
al momento che seleziono un Item dalla prima ListBox() non mi si popola la seconda, vorrei popolare la seconda dato l'ID della prima, come devo fare ??? |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Feb 2001
Città: Nordest
Messaggi: 217
|
In Page_Load non hai salvato Id, lo hai solo memorizzato temporaneamente nella variabile ID che poi verrà sovrascritta dal ciclo successivo.
Soluzione: scrivi una classe che derivi da ListViewItem e aggiungi una proprietà ItemData per memorizzare questo dato. Codice:
Public Class MyListViewItem
Inherits ListViewItem
Private m_itemdata As Long
Public Property ItemData() As Long
Get
Return m_itemdata
End Get
Set(ByVal Value As Long)
m_itemdata = Value
End Set
End Property
End Class
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'popolo la ListBox Categorie
Dim Cat As String
Dim mlvi As MyListViewItem
conn.Open()
Dim sql As String = "SELECT * FROM Categorie"
Dim comm As New OleDbCommand(sql, conn)
Dim read As OleDbDataReader = comm.ExecuteReader
While read.Read()
mlvi = New MyListViewItem
mlvi.ItemData = read("Id")
mlvi.Text = read("Cat")
lsbCat.Items.Add(mlvi)
End While
conn.Close()
End Sub
|
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Aug 2000
Messaggi: 1209
|
OK perfetto!!!
ti ringrazio per ora , ciao!!! |
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Aug 2000
Messaggi: 1209
|
Inherits ListViewItem
mi dice "Non definito"....... ma se lo tolgo??? |
|
|
|
|
|
#5 |
|
Senior Member
Iscritto dal: Aug 2000
Messaggi: 1209
|
Ho ripreso questa classe:
Codice:
Private m_id As Long
Private m_text As String
Public Overrides Function ToString() As String
Return m_text
End Function
Public Sub New(ByVal Text As String, ByVal Value As Long)
m_text = Text
m_id = Value
End Sub
Public Property Value() As Long
Get
Return m_id
End Get
Set(ByVal Value As Long)
m_id = Value
End Set
End Property
Public Property Text() As String
Get
Return m_text
End Get
Set(ByVal Value As String)
m_text = Value
End Set
End Property
ID - Categoria - IDC con la classe che ho inserito qui sopra come posso raccogliere i tre valori e poi gestirmeli ??? |
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Feb 2001
Città: Nordest
Messaggi: 217
|
Non puoi usare questa classe come con un controllo ListBox.
Con la ListView, o usi la classe predefinita ListViewItem, oppure crei una classe ereditata da ListViewItem. |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 14:42.



















