DataBinding In a Custom Control

Discussion in 'ASP .Net Web Controls' started by thoughtcrimes, Dec 5, 2007.

  1. 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
     
    thoughtcrimes, Dec 5, 2007
    #1
    1. Advertising

  2. On Dec 5, 12:38 pm, thoughtcrimes <> 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(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
     
    thoughtcrimes, Dec 5, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SW1hciBTcGFhbmphYXJz?=

    Databinding to custom properties of a custom class in ASP.NET 2

    =?Utf-8?B?SW1hciBTcGFhbmphYXJz?=, Apr 20, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    596
    =?Utf-8?B?SW1hciBTcGFhbmphYXJz?=
    Apr 20, 2005
  2. jobs
    Replies:
    0
    Views:
    1,684
  3. Matt Sokol
    Replies:
    2
    Views:
    409
    Theo Tillotson
    Aug 7, 2003
  4. Scott

    Databinding a custom user control

    Scott, Dec 4, 2003, in forum: ASP .Net Building Controls
    Replies:
    4
    Views:
    136
  5. Earl Teigrob
    Replies:
    1
    Views:
    423
    Shravan
    Mar 1, 2004
Loading...

Share This Page