Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > DataBinding In a Custom Control

Reply
Thread Tools

DataBinding In a Custom Control

 
 
thoughtcrimes
Guest
Posts: n/a
 
      12-05-2007
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
 
Reply With Quote
 
 
 
 
thoughtcrimes
Guest
Posts: n/a
 
      12-05-2007
On Dec 5, 12:38 pm, thoughtcrimes <(E-Mail Removed)> wrote:
> 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(dataI tem,
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Databinding expressions are only supported on objects that have a DataBinding event jobs ASP .Net 0 09-25-2007 11:54 PM
Gridview encoding, or how to run commands before gridview's default databinding, or, how do I disable default databinding at all? Sergei Shelukhin ASP .Net 1 11-12-2006 01:43 PM
Databinding to custom properties of a custom class in ASP.NET 2 =?Utf-8?B?SW1hciBTcGFhbmphYXJz?= ASP .Net 0 04-20-2005 07:34 PM
DataGrid Custom Column Error when DataBinding "does not contain a definition for 'DataBinding'" Earl Teigrob ASP .Net Datagrid Control 1 03-01-2004 04:52 AM
ControlDesigner not invoked on custom control when control is rendered within another custom control Matt Sokol ASP .Net Building Controls 2 08-07-2003 07:13 AM



Advertisments