HighTechTalks DotNet Forums  

DataBinding In a Custom Control

ASP.net Web Controls microsoft.public.dotnet.framework.aspnet.webcontrols


Discuss DataBinding In a Custom Control in the ASP.net Web Controls forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
thoughtcrimes
 
Posts: n/a

Default DataBinding In a Custom Control - 12-05-2007 , 11:38 AM






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
  #2  
Old   
thoughtcrimes
 
Posts: n/a

Default Re: DataBinding In a Custom Control - 12-05-2007 , 12:49 PM






On Dec 5, 12:38 pm, thoughtcrimes <tylerxpr... (AT) gmail (DOT) com> wrote:
Quote:
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 Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.