Guys,

I have used this class in VB.NET code to create dataitems that I can add to comboboxes, listboxes etc. I can display the text item, and still get hold of other database fields associated with that row by using . .

Ctype(cb_names.selecteditem,dataitem).Altfield1 for example.

Can I do something like this in ASP.NET ? I have tried using this but I'm getting an overload resolution fail when adding items to a listbox.

Basically, I need to add items to a list box from a datatable, I need to display the text field, but retain the id field so I know which record the users are selecting.

Am I going about this the right way ?

Cheers
Bob

VB Code:
  1. Public Class DataItem
  2.     Public id As Integer
  3.     Public text As String
  4.     Public row As DataRow
  5.  
  6.     Public Overrides Function ToString() As String
  7.         Return text
  8.     End Function
  9.     Public Sub New(ByVal row As DataRow)
  10.         Me.new(row, "id", "name")
  11.     End Sub
  12.     Public Sub New(ByVal id As Integer, ByVal text As String)
  13.         Me.id = id
  14.         Me.text = text
  15.     End Sub
  16.     Public Sub New(ByVal row As DataRow, ByVal id As Integer, ByVal text As String)
  17.         Me.row = row
  18.         Me.id = id
  19.         Me.text = text
  20.     End Sub
  21.     Public Sub New(ByVal row As DataRow, ByVal idfield As String, ByVal textfield As String)
  22.         id = Nz(row(idfield), 0)
  23.         text = Nz(row(textfield), "")
  24.         Me.row = row
  25.     End Sub

This is an example of how I've used it in VB.NET . . .

VB Code:
  1. cb_rfiissuedby.Items.Clear()
  2.                     dt = db.ContractStaff.Table(CurrentContract.ID)
  3.                     For Each row As DataRow In dt.Rows
  4.                         cb_rfiissuedby.Items.Add(New DataItem2(row, "ID", "Name", "CompanyName"))
  5.                     Next
  6.                     dt.Dispose()
  7.                     dt = Nothing