Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Paul Griffiths 370 posts 1021 karma points
    Feb 25, 2014 @ 11:48
    Paul Griffiths
    0

    Binding umbraco data to a dropdown control

    Hello all,

    I’m wondering if someone can help me with regards to binding umbraco data to a dropdown control.

    I would like to create a datatype that dynamically populates the values of a dropdown list with member properties that are not standard umbraco fields.

    I have a property type called Venue name and I would like to display the members name (is a standard field) and the venue name (not standard) in the list. For example

    Paul Griffiths -- The Grosvenor

    I have tried two methods to achieve this which are as follows. Firstly I installed uComponents and tried the SQL DropDownList datatytpe and using the following SQL I was able to query the database and join and fetch back both the Login name and Email (both standard umbraco fields)

    SELECT (LoginName +' - '+ Email) as 'Text', Nodeid as 'Value' FROM cmsMember
    
    INNER JOIN cmsMember2MemberGroup
    
    ON cmsMember.Nodeid = cmsMember2MemberGroup.Member
    
    WHERE cmsMember2MemberGroup.MemberGroup = '1134'
    

    This works fine but I could do with replacing the email field with venue name but I don’t know where to find the data in the database.

    With being unable to target the fields I wanted, for my second attempt I tried to create a user control and wrap it in the umbraco user control wrapper but still I have some issues. I have added the following code to user control

       <%@ Control Language="C#" AutoEventWireup="true" CodeFile="VenueOwners.ascx.cs" Inherits="UserControls_VenueOwners" %>
    
    
    <asp:DropDownList ID="ddlVenueOwners" DataTextField="Name" DataValueField="Id" runat="server" AppendDataBoundItems="true">
        <asp:ListItem Text="Select Venue Owner" Value=""/>
    </asp:DropDownList>
    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using umbraco.editorControls.userControlGrapper;
    using System.Web.UI.WebControls;
    using System.Web.Security;
    using umbraco.businesslogic;
    using umbraco.cms.businesslogic.member;
    using umbraco.cms.businesslogic.propertytype;
    
    public partial class UserControls_VenueOwners : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
                var memberGroup = MemberGroup.GetByName("Venue Owners");
                ddlVenueOwners.DataValueField = "Id";
                ddlVenueOwners.DataTextField = "Name";
                foreach (Member member in memberGroup.GetMembers())
                {
                    ddlVenueOwners.DataSource = new[]
                    {
                        new {Id = member.getProperty("Id"), Name = member.getProperty("Address")}
                    };
    
                }
                ddlVenueOwners.DataBind();
        }
    
        public object value
        {
            get { return ddlVenueOwners.SelectedValue; }
            set
            {
                ddlVenueOwners.SelectedValue = (value != null) ? value.ToString() : string.Empty;
            }
    
        }
    }
    

    For testing purposes I have not yet wrapped this up in the umbraco user control wrapper and I have place a macro in a template to see if the user control rendered ok, which it does. However, with using this method I can’t get the fields to populate at all.

    There is some material already on the our forum which I have tried to follow but maybe I’m missing something. Can someone please help me and maybe point out where I may be going wrong.

    Sorry for the length of this post but I wanted to really make my problem clear.

    Thanks in advance

    Paul

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Feb 25, 2014 @ 19:17
    Lee Kelleher
    0

    Hi Paul,

    I think the issue with the code snippet is that the ddlVenueOwners.DataSource is being set with a single value within the foreach loop.

    Try replacing the foreach loop with this...

    ddlVenueOwners.DataSource = memberGroup
        .GetMembers()
        .Select(member => new {
            Id = member.getProperty("Id"),
            Name = member.getProperty("Address")
        });
    

    Not sure where the "Id" property is a field, or should it be member.Id?

    Cheers,
    - Lee

  • Paul Griffiths 370 posts 1021 karma points
    Feb 25, 2014 @ 20:33
    Paul Griffiths
    0

    Hi Lee,

    Thanks a bunch for your reply mate really appreciate it ;) as been pulling my hair out all day with this one but it may be my inexperience showing as things are all relatively new so please excuse me if i miss the obvious. I have tried replacing the code that you suggested but I still cant seem to populate the Dropdown list :(

    Heres the code with the one you suggested

    using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.UI;
        using umbraco.editorControls.userControlGrapper;
        using System.Web.UI.WebControls;
        using System.Web.Security;
        using umbraco.businesslogic;
        using umbraco.cms.businesslogic.member;
        using umbraco.cms.businesslogic.propertytype;
    
    public partial class UserControls_VenueOwners : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
            var memberGroup = MemberGroup.GetByName("Venue Owners");
            ddlVenueOwners.DataValueField = "Id";
            ddlVenueOwners.DataTextField = "Name";
            ddlVenueOwners.DataSource = memberGroup.GetMembers().Select(member => new {Id = member.getProperty("Id"), Name = member.getProperty("Address")});
    
            ddlVenueOwners.DataBind();
        }
    
        public object value
        {
            get { return ddlVenueOwners.SelectedValue; }
            set
            {
                ddlVenueOwners.SelectedValue = (value != null) ? value.ToString() : string.Empty;
            }
    
        }
    }
    

    Thank you mate

    Paul

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Feb 26, 2014 @ 12:11
    Lee Kelleher
    0

    Hi Paul,

    Maybe the linq statement was a bit too much (sorry), let's move the data elsewhere...

    ddlVenueOwners.DataValueField = "Key";
    ddlVenueOwners.DataTextField = "Value";
    ddlVenueOwners.DataSource = GetVenueOwners("Venue Owners");
    

    then we can work with the data within its own method:

    private Dictionary<string, string> GetVenueOwners(string memberGroupName)
    {
        var items = new Dictionary<string, string>();
        var memberGroup = MemberGroup.GetByName(memberGroupName);
    
        if (memberGroup != null)
        {
            foreach(member in memberGroup.GetMembers())
            {
                var id = member.getProperty("Id");
                var address = member.getProperty("Address");
                items.Add(id, address);
            }
        }
    
        return items;
    }
    

    See how that works for you?

    Cheers,
    - Lee

  • Paul Griffiths 370 posts 1021 karma points
    Feb 26, 2014 @ 12:54
    Paul Griffiths
    0

    Hi Lee, Thanks again for your reply. Still getting errors. Its not recognising 'member' in the foreach loop

    Here's how I have it in the code behind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using umbraco.editorControls.userControlGrapper;
    using System.Web.UI.WebControls;
    using System.Web.Security;
    using umbraco.businesslogic;
    using umbraco.cms.businesslogic.member;
    using umbraco.cms.businesslogic.propertytype;
    
    public partial class UserControls_VenueOwners : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
            ddlVenueOwners.DataValueField = "Key";
            ddlVenueOwners.DataTextField = "Value";
            ddlVenueOwners.DataSource = GetVenueOwners("Venue Owners");
    
        }  
    
        private Dictionary<string, string> GetVenueOwners(string memberGroupName)
            {
    
                var items = new Dictionary<string, string>();
                var memberGroup = MemberGroup.GetByName(memberGroupName);
                var member = memberGroup.GetMembers();
    
                if (memberGroup != null)
                {
                    foreach(member in memberGroup.GetMembers())
                    {
                        var id = member.Id;
                        var address = member.getProperty("Address");
                        items.Add(id, address);
                    }
                }
    
                return items;
            }
    
    
    
    
        //public object value
        //{
        //    get { return ddlVenueOwners.SelectedValue; }
        //    set
        //    {
        //        ddlVenueOwners.SelectedValue = (value != null) ? value.ToString() : string.Empty;
        //    }
    
        //}
    }
    
  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Feb 26, 2014 @ 13:01
    Lee Kelleher
    0

    Ah, the foreach(member should be foreach(var member ... I coded it off the top of my head, didn't spot the error.

  • Paul Griffiths 370 posts 1021 karma points
    Feb 26, 2014 @ 13:25
    Paul Griffiths
    0

    Hi Lee,

    I have changed that now and it is sorted that problem but now it doesn't like the items.Add(id, address); line.

    Please see the following errors.

    enter image description here

    Thanks for your help mate its bending my head lol.

    Paul

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Feb 26, 2014 @ 13:27
    Lee Kelleher
    0

    Change this: items.Add(id, address); to items.Add(id.ToString(), address);

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Feb 26, 2014 @ 13:28
    Lee Kelleher
    0

    ... oh and change var address = member.getProperty("Address"); to var address = member.getProperty("Address").Value;

    Cheers,
    - Lee

  • Paul Griffiths 370 posts 1021 karma points
    Feb 26, 2014 @ 14:07
    Paul Griffiths
    0

    Hi Lee,

    Finally getting somewhere now :) my code is not breaking anymore but Im still failing to pull back the data to populate the dropdown list. Heres my code I have changed the address field to venueName. I also needed to convert both parameters .tostring in the items.Add().

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using umbraco.editorControls.userControlGrapper;
    using System.Web.UI.WebControls;
    using System.Web.Security;
    using umbraco.businesslogic;
    using umbraco.cms.businesslogic.member;
    using umbraco.cms.businesslogic.propertytype;
    
    public partial class UserControls_VenueOwners : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
            ddlVenueOwners.DataValueField = "Id";
            ddlVenueOwners.DataTextField = "Name";
            ddlVenueOwners.DataSource = GetVenueOwners("Venue Owners");
    
        }  
    
        private Dictionary<string, string> GetVenueOwners(string memberGroupName)
            {
    
                var items = new Dictionary<string, string>();
                var memberGroup = MemberGroup.GetByName(memberGroupName);
    
                if (memberGroup != null)
                {
                    foreach(var member in memberGroup.GetMembers())
                    {
                        var id = member.Id;
                        var name = member.getProperty("venueName").Value;
                        items.Add(id.ToString(), name.ToString());
                    }
                }
    
                return items;
            }
    
    
    
    
        public object value
        {
            get { return ddlVenueOwners.SelectedValue; }
            set
            {
                ddlVenueOwners.SelectedValue = (value != null) ? value.ToString() : string.Empty;
            }
    
        }
    }
    

    Any Ideas mate

    Thanks again ;)

    Paul

  • Paul Griffiths 370 posts 1021 karma points
    Feb 26, 2014 @ 14:15
    Paul Griffiths
    0
  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Feb 26, 2014 @ 14:37
    Lee Kelleher
    0

    Are you getting anything back?
    Is the dropdown list completely empty? or has a bunch of empty options?
    Are you using Visual Studio? Can you debug through the code, see what's causing any issues?

    Is the member group literally called "Venue Owners"? Does it have any members in that group?

    Basically, just need to check if you have the data in place?

    Cheers,
    - Lee

  • Paul Griffiths 370 posts 1021 karma points
    Feb 26, 2014 @ 15:58
    Paul Griffiths
    0

    Hi Lee,

    Been messing with this all day but still no luck. The issue I was having was that it wasnt pulling back any data. After spotting a few silly mistakes I have reverted back to the original code that you posted and I have got it working so that it populates the dropdown list perfect through a macro :) happy days.

    However, when I try and wrap the working user control up using the user control wrapper datatype and use it as a datatype in a document type its falling over and complaining about the ddlVenueOwners.Databaind() line

    Here is my code at the moment.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using umbraco.editorControls.userControlGrapper;
    using System.Web.UI.WebControls;
    using System.Web.Security;
    using umbraco.businesslogic;
    using umbraco.cms.businesslogic.member;
    using umbraco.cms.businesslogic.propertytype;
    
    public partial class UserControls_VenueOwners : System.Web.UI.UserControl, 
        umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
    
                var memberGroup = MemberGroup.GetByName("Venue Owners");
                ddlVenueOwners.DataValueField = "Id";
                ddlVenueOwners.DataTextField = "Name";
                ddlVenueOwners.DataSource = memberGroup.GetMembers().Select(member => new
                {
                    Id = member.Id,
                    Name = member.Text.ToString() + " - " + member.Id
    
                });
                ddlVenueOwners.DataBind();
    
            }
        }
    
    
    
        public object value
        {
            get { return ddlVenueOwners.SelectedValue; }
            set
            {
                ddlVenueOwners.SelectedValue = (value != null) ? value.ToString() : string.Empty;
            }
    
        }
    }
    

    enter image description here

    Again thanks for your time mate.

    Ps yea the meber group in called venue owners (with a space) and it has members present and yeah mate im using vs 2013.

    Paul

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Feb 26, 2014 @ 16:10
    Lee Kelleher
    100

    Hi Paul,

    This code, it checks if the selected value exists in the drop-down list:

    public object value
    {
        get { return ddlVenueOwners.SelectedValue; }
        set
        {
            if (value != null)
            {
                var selectedValue = value.ToString();
    
                if (ddlVenueOwners.Items.FindByValue(selectedValue) != null)
                {
                    ddlVenueOwners.SelectedValue = selectedValue;
                }
            }
        }
    }
    

    Cheers,
    - Lee

  • Paul Griffiths 370 posts 1021 karma points
    Feb 26, 2014 @ 16:22
    Paul Griffiths
    0

    Lee,

    You my friend are a legend!! :) there is no way I could of achieved that without your help. It is now doing exactly what I want it to do. Perfect!!

    Really appreciate you giving up your time to help me.

    Here is my final code just in case it helps anyone else

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using umbraco.editorControls.userControlGrapper;
    using System.Web.UI.WebControls;
    using System.Web.Security;
    using umbraco.businesslogic;
    using umbraco.cms.businesslogic.member;
    using umbraco.cms.businesslogic.propertytype;
    
    public partial class UserControls_VenueOwners : System.Web.UI.UserControl, 
        umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
    
                var memberGroup = MemberGroup.GetByName("Venue Owners");
                ddlVenueOwners.DataValueField = "Id";
                ddlVenueOwners.DataTextField = "Name";
                ddlVenueOwners.DataSource = memberGroup.GetMembers().Select(member => new
                {
                    Id = member.Id,
                    Name = member.Text.ToString() + " - " + member.Id
    
                });
                ddlVenueOwners.DataBind();
    
            }
        }
    
    
        public object value
        {
    
            get { return ddlVenueOwners.SelectedValue; }
            set
            {
                if (value != null)
                {
                    var selectedValue = value.ToString();
    
                    if (ddlVenueOwners.Items.FindByValue(selectedValue) != null)
                    {
                        ddlVenueOwners.SelectedValue = selectedValue;
                    }
                }
            }
    
        }
    }
    

    Thank you

    Paul

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Feb 26, 2014 @ 16:25
    Lee Kelleher
    1

    Cool, glad we got there in the end!

    (Don't forgot to mark the solution - for future reference)

    Cheers,
    - Lee

  • Paul Griffiths 370 posts 1021 karma points
    Feb 26, 2014 @ 16:29
    Paul Griffiths
    0

    In the end ;) ha ha

    will do cheers mate!

    Paul

  • Paul Griffiths 370 posts 1021 karma points
    Feb 27, 2014 @ 10:57
    Paul Griffiths
    0

    Hi Lee,

    I was just wondering if you could help with one last thing that I realised last night. The dropdown list is populationg with the relevant data, however when I select a value and save and publish the node, navigate away from the node then revisit the dropdown list doesn't preserve the value a;although it is saved in the data base.

    I have tried this but doesnt seem to like the Node currentPage = Node.GetCurrent(); line

    Node currentPage = Node.GetCurrent();
    
    if (!IsPostBack)
    {
        ddlVenueOwners.SelectedValue = currentPage.GetProperty("venueOwner").Value;
    
    }
    

    Thanks again mate

    Paul

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Feb 27, 2014 @ 11:01
    Lee Kelleher
    0

    Where is that code snippet? Is it in the value property?

    Not sure why you'd need to access the currentPage, the selected value should be in the value itself?
    (I know, too many mentions of "value")

  • Paul Griffiths 370 posts 1021 karma points
    Feb 27, 2014 @ 11:10
    Paul Griffiths
    0

    I placed that code in the page load event mate should it be in the value?

    Only reason I have used current page is because i used it on some previous code and it worked lol.

    Just a random attempt i suppose.

    Cheers

    Paul

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Feb 27, 2014 @ 11:16
    Lee Kelleher
    0

    Take a read over this blog post, it covers the idea behind the user-control wrapper data-types:
    http://www.nibble.be/?p=24

    The key part here is that when the value property is set, then the selected value will also need to be saved in a private field (so you can reuse it later in the Page_Load event).

    Hope that makes sense?

    Cheers,
    - Lee

  • Paul Griffiths 370 posts 1021 karma points
    Feb 27, 2014 @ 15:36
    Paul Griffiths
    0

    Hi Lee,

    Thanks for the advice and link but I managed to sort the issue using a thread i stumbled on of yours, which really helped.

    http://our.umbraco.org/forum/developers/extending-umbraco/4230-Persisting-DropDownList-on-PostBack-%28in-custom-sectiontree-page%29

    Thanks again for your help my dropdown lists are now storing the values :)

    Paul

Please Sign in or register to post replies

Write your reply to:

Draft