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 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;
}
}
}
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;
}
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;
// }
//}
}
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;
}
}
}
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?
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;
}
}
}
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.
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;
}
}
}
}
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;
}
}
}
}
}
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
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).
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)
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
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
Hi Paul,
I think the issue with the code snippet is that the
ddlVenueOwners.DataSource
is being set with a single value within theforeach
loop.Try replacing the
foreach
loop with this...Not sure where the "Id" property is a field, or should it be
member.Id
?Cheers,
- Lee
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
Thank you mate
Paul
Hi Paul,
Maybe the linq statement was a bit too much (sorry), let's move the data elsewhere...
then we can work with the data within its own method:
See how that works for you?
Cheers,
- Lee
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:
Ah, the
foreach(member
should beforeach(var member
... I coded it off the top of my head, didn't spot the error.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.
Thanks for your help mate its bending my head lol.
Paul
Change this:
items.Add(id, address);
toitems.Add(id.ToString(), address);
... oh and change
var address = member.getProperty("Address");
tovar address = member.getProperty("Address").Value;
Cheers,
- Lee
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().
Any Ideas mate
Thanks again ;)
Paul
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
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.
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
Hi Paul,
This code, it checks if the selected value exists in the drop-down list:
Cheers,
- Lee
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
Thank you
Paul
Cool, glad we got there in the end!
(Don't forgot to mark the solution - for future reference)
Cheers,
- Lee
In the end ;) ha ha
will do cheers mate!
Paul
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
Thanks again mate
Paul
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 thevalue
itself?(I know, too many mentions of "value")
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
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 isset
, then the selected value will also need to be saved in aprivate
field (so you can reuse it later in thePage_Load
event).Hope that makes sense?
Cheers,
- Lee
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
is working on a reply...