DataBinding In a Custom Control

T

thoughtcrimes

Hello:

I am building a DropDownList-type control. I opted for inheriting just
the WebControl class instead of DropDownList class because the child
controls and the rendered HTML will be quite different.

I have an IEnumerable DataSource and the properties DataValueField and
DataTextField which are just strings of the name of properties in
whatever type of objects are in the DataSource. What I would like to
do is loop through the DataSource and create ListItems by copying over
the DataValueFields and DataTextFields to the ListItems' Value and
Text Fields, but I don't know how to do this.

So if I had a class "Cola" with properties "ColaId" and "ColaName" and
set the DataSource to a List (Of Cola) and the DataValueField to
"ColaId" and the DataTextField to "ColaName", and I ran the following
code:

----------
Dim items As New ArrayList()
Dim data As IEnumerable = GetDataSource()

For Each dataItem As Object In data
Dim listItem As New myListItem
listItem.Value = '' ???
listItem.Text = '' ???
Next

Dim itemsCollection As New myListItemsCollection(items)
----------

I would want to end up with an myListItemsCollection containing
several myListItems that had Values and Text s from the List (Of
Cola).

Any help or links would be very much appreciated.

Thanks,


-Tyler
 
T

thoughtcrimes

Hello:

I am building a DropDownList-type control. I opted for inheriting just
the WebControl class instead of DropDownList class because the child
controls and the rendered HTML will be quite different.

I have an IEnumerable DataSource and the properties DataValueField and
DataTextField which are just strings of the name of properties in
whatever type of objects are in the DataSource. What I would like to
do is loop through the DataSource and create ListItems by copying over
the DataValueFields and DataTextFields to the ListItems' Value and
Text Fields, but I don't know how to do this.

So if I had a class "Cola" with properties "ColaId" and "ColaName" and
set the DataSource to a List (Of Cola) and the DataValueField to
"ColaId" and the DataTextField to "ColaName", and I ran the following
code:

----------
Dim items As New ArrayList()
Dim data As IEnumerable = GetDataSource()

For Each dataItem As Object In data
Dim listItem As New myListItem
listItem.Value = '' ???
listItem.Text = '' ???
Next

Dim itemsCollection As New myListItemsCollection(items)
----------

I would want to end up with an myListItemsCollection containing
several myListItems that had Values and Text s from the List (Of
Cola).

Any help or links would be very much appreciated.

Thanks,

-Tyler

Aha! The DataBinder seems to work well.

listItem.Value =
Convert.ToString(DataBinder.GetPropertyValue(dataItem,
DataValueField))

I don't know if the exception it throws is all that nice, so might
try, catch and throw my own if the DataValueField doesn't exist in the
dataItem object.

If anyone has any links to good tutorials or references on building
custom WebControls please reply.

Thanks,

-Tyler
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top