Results 1 to 3 of 3

Thread: [RESOLVED] Dataitem class

  1. #1

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Resolved [RESOLVED] Dataitem class

    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

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Dataitem class

    The reason you can't do this is because of the nature of the web control that is eventually rendered. A combobox/listbox can only have text in it, or ListItems which hold a "text" value and "value" value.

    You'd have to do something like this:

    VB Code:
    1. Me.DropDownList1.Items.Add(New ListItem("Show This", "17"))

    Of course, instead of "Show This", you could have something like MyObject.SomeField.ToString(), but it's limited to just these two. No objects.

  3. #3

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: Dataitem class

    Mendhak, thankyou.

    The value and text fields of a listbox / ddlist will suit just fine.

    Ta
    Bob

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width