|
-
Aug 10th, 2005, 09:41 AM
#1
Thread Starter
Fanatic Member
[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:
Public Class DataItem
Public id As Integer
Public text As String
Public row As DataRow
Public Overrides Function ToString() As String
Return text
End Function
Public Sub New(ByVal row As DataRow)
Me.new(row, "id", "name")
End Sub
Public Sub New(ByVal id As Integer, ByVal text As String)
Me.id = id
Me.text = text
End Sub
Public Sub New(ByVal row As DataRow, ByVal id As Integer, ByVal text As String)
Me.row = row
Me.id = id
Me.text = text
End Sub
Public Sub New(ByVal row As DataRow, ByVal idfield As String, ByVal textfield As String)
id = Nz(row(idfield), 0)
text = Nz(row(textfield), "")
Me.row = row
End Sub
This is an example of how I've used it in VB.NET . . .
VB Code:
cb_rfiissuedby.Items.Clear()
dt = db.ContractStaff.Table(CurrentContract.ID)
For Each row As DataRow In dt.Rows
cb_rfiissuedby.Items.Add(New DataItem2(row, "ID", "Name", "CompanyName"))
Next
dt.Dispose()
dt = Nothing
-
Aug 11th, 2005, 05:33 AM
#2
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:
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.
-
Aug 11th, 2005, 05:39 AM
#3
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|