SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Guru pinch's Avatar
    Join Date
    Mar 2005
    Posts
    688
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    DropDownList Item Binding

    I am binding my dropdownlist to an objectdatasource which returns a collection 'Player' objects. A 'Player' object has 'FirstName', 'MiddleName', and 'LastName' properties, but also has a lazy-load 'FullName' property. I cannot specify 'FullName' as the DataTextField property because it isn't available when the objectdatasource passes the object to the dropdownlist.

    So, I figured there must be an event in the dropdownlist which allows you to examine each item being bound; here I could concatenate the 'FirstName', 'MiddleName', and 'LastName' properties and set this 'Full Name' as the Text property of each item being bound in the dropdownlist. However, there is no 'item'databound event for dropdownlist and apparently there is nothing to bind to in the DataBound property (the e argument in the event is basically empty).

    I really don't feel like modifying my Player object for this one case, is there any way to modify the dropdownlist items as they're being bound?

  2. #2
    SitePoint Wizard
    Join Date
    Feb 2007
    Posts
    1,274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    could you explain what you mean by "lazy loading" in this context?

  3. #3
    SitePoint Guru pinch's Avatar
    Join Date
    Mar 2005
    Posts
    688
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a property in my Player class (BLL) that isn't in my PlayerDetails entity object (which is basically a method-less container object for passing information between my DAL and BLL layers). My PlayerDetails entity class has properties:

    PlayerID
    FirstName
    MiddleName
    LastName
    TeamCode
    FirstYear
    Number

    Now, my Player class (in my BLL) has an additional property called 'FullName' which is only loaded when the property is requested.

    going off of memory here....
    [code]
    private string _fullName = String.Empty
    public string FullName
    {
    get {
    if(_fullName == String.Empty) {
    _fullName = _firstName + _middleName + _lastName
    return _fullName
    }
    }

    }

    So if I already have a Player object I can access this property to get the player's full name. But, since this property is not initialized in the constructor (I'm not realizing this may be a solution) it isn't available when my ODS calls its select method. The select method only returns a Player object, whose FullName property has not yet been loaded.

    Sound right?

  4. #4
    SitePoint Wizard
    Join Date
    Feb 2007
    Posts
    1,274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And how does the databinding definition look like in the aspx?

  5. #5
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,633
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Thats not quite lazy loading--which usually implies a secondary database hit. That is just a readonly property, and you should be able to access that using databinding without any extra hoo ha. Just something like:

    Code:
    <asp:DropDownList runat="Server" ID="whutever" DataSourceID="MyODS" DataTextField="FullName" DataValueField="UserID" />
    That said, there are so many other issues with DDLs that 75&#37; of the time I build them from codebehind. Or at least bind the selected value from codebehind.

  6. #6
    SitePoint Guru pinch's Avatar
    Join Date
    Mar 2005
    Posts
    688
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats not quite lazy loading--which usually implies a secondary database hit. That is just a readonly property, and you should be able to access that using databinding without any extra hoo ha. Just something like:
    Ah didn't know that. As far as just binding to it, it doesn't work. My Select Method just returns a collection of Player objects to the DDL, and these objects have not yet loaded the FullName property because it is only loaded when you ask for it as indicated in the code above. I'll post the code a bit later when I get access.

    And how does the databinding definition look like in the aspx?
    I'll post the code when I get home tonight, don't have access to it right now.

    That said, there are so many other issues with DDLs that 75&#37; of the time I build them from codebehind. Or at least bind the selected value from codebehind.
    That's interesting, what kind of issues are you referring to? I had a problem last week that never got resolved and it had to do with binding DDLs to ODSs and I wonder if I'm up against something similar.

  7. #7
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,633
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Ah didn't know that. As far as just binding to it, it doesn't work. My Select Method just returns a collection of Player objects to the DDL, and these objects have not yet loaded the FullName property because it is only loaded when you ask for it as indicated in the code above. I'll post the code a bit later when I get access.
    True, the value is created on-demand. But things like this work just fine with databinding--all the databinder does is effectively call get_FullName at the end of the day. I suspect there is another issue that is breaking this for you.

    Biggest issue with drop down lists is that they don't handle unmatched selected values well, and, if you do things declaratively, it is tricky to "catch" this before it happens, usually leading to a Yellow Screen of Death. Lack of support for some fancier ways to handle the lists (optiongroups) also plays in.

  8. #8
    SitePoint Guru pinch's Avatar
    Join Date
    Mar 2005
    Posts
    688
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    True, the value is created on-demand. But things like this work just fine with databinding--all the databinder does is effectively call get_FullName at the end of the day. I suspect there is another issue that is breaking this for you.
    Your suspicion was correct, when I got home and tried it out again it worked so it must have been something else that was going wrong.

    So, the Player objects returned from the ODS are true objects in memory and I'm guessing that because of this each object will still have access to the FullName property. Is there another name for this if it isn't lazy-load?

  9. #9
    SitePoint Wizard
    Join Date
    Feb 2007
    Posts
    1,274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Calculated Property


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •